trim
This page explains how to use the trim function in APL.
Introduction
The trim function removes all leading and trailing characters from a string that are part of a specified cutset. A cutset is a set of characters, and trim removes any of them if they appear at the beginning or end of the string.
Use the trim function when you want to normalize or clean string values by stripping unwanted characters such as quotes, spaces, slashes, or punctuation. It’s useful in log analysis, standardizing OpenTelemetry attributes, or cleaning identifiers in security logs.
Usage
Syntax
trim(cutset, source)Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| cutset | string | ✓ | The set of characters to remove from both the beginning and end of source. |
| source | string | ✓ | The source string to process. |
Returns
A string with all leading and trailing characters removed that match any character in the cutset.
Use case examples
You can use trim to normalize URLs by removing leading and trailing slashes before grouping.
Query
Output
| clean_uri | count |
|---|---|
| api/login | 120 |
| product/details | 85 |
| cart/add | 62 |
This query removes leading and trailing slashes from the uri field so that identical paths group consistently.
In traces, you can use trim to standardize service names by removing surrounding underscores or dashes.
Query
Output
| clean_service | avg_duration |
|---|---|
| frontend | 120ms |
| cart | 210ms |
| checkout | 310ms |
This query ensures service names are consistent before calculating averages.
When analyzing user IDs, you can use trim to remove unwanted wrapping characters, such as hashes or quotes.
Query
Output
| clean_id | count |
|---|---|
| user123 | 42 |
| user456 | 38 |
| user789 | 55 |
This query strips hashes around user IDs so they can be counted reliably.