trim()
The trim() function removes all leading and trailing whitespace characters (spaces, tabs, newlines, etc.) from a string and returns the result.
Syntax
trim(STR)
Parameters
STR- The string field or value to remove whitespace from. Non-string values are converted using
toString()before processing.
Description
The trim() function removes all leading and trailing whitespace from STR and returns the result. It operates identically to Java's String.trim() method, removing characters with Unicode code points at or below U+0020.
- If
STRisnull, the function returnsnull. - Whitespace in the middle of the string is not removed.
Error codes
N/A
Usage examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Remove leading and trailing whitespace from a URI
table limit=5 WEB_APACHE_SAMPLE | eval result = trim(uri) | fields uri, result -
Remove whitespace from a string with leading and trailing spaces
json "{'val': ' hello world '}" | eval result = trim(val) | # result: "hello world" -
NULL input
json "{'val': null}" | eval result = trim(val) | # result: null
Compatibility
The trim() function is available since before Sonar 4.0.