trim()

Removes all leading and trailing matches of the specified regular expression.

Syntax

trim(regex, source)

Learn more about syntax conventions.

Parameters

Name Type Required Description
regex string ✔️ The string or regular expression to be trimmed from source.
source string ✔️ The source string from which to trim regex.

Returns

source after trimming matches of regex found in the beginning and/or the end of source.

Examples

Trim specific substring

The following statement trims substring from the start and the end of the string_to_trim.

let string_to_trim = @"--https://bing.com--";
let substring = "--";
print string_to_trim = string_to_trim, trimmed_string = trim(substring,string_to_trim)

Output

string_to_trim trimmed_string
--<https://bing.com--> <https://bing.com>

Trim non-alphanumeric characters

The next statement trims all non-word characters from start and end of the string.

range x from 1 to 5 step 1
| project str = strcat("-  ","Te st",x,@"// $")
| extend trimmed_str = trim(@"[^\w]+",str)

Output

str trimmed_str
- Te st1// $ Te st1
- Te st2// $ Te st2
- Te st3// $ Te st3
- Te st4// $ Te st4
- Te st5// $ Te st5

Trim whitespaces

The next statement trims all spaces from start and end of the string.

let string_to_trim = @"    Hello, world!    ";
let substring = @"\s+";
print
    string_to_trim = string_to_trim,
    trimmed_string = trim(substring, string_to_trim)

Output

string_to_trim trimmed_string
Hello, world! Hello, world!