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 STR is null, the function returns null.
  • 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.

  1. Remove leading and trailing whitespace from a URI

    table limit=5 WEB_APACHE_SAMPLE | eval result = trim(uri) | fields uri, result
    
  2. Remove whitespace from a string with leading and trailing spaces

    json "{'val': '  hello world  '}" | eval result = trim(val)
    | # result: "hello world"
    
  3. NULL input

    json "{'val': null}" | eval result = trim(val)
    | # result: null
    

Compatibility

The trim() function is available since before Sonar 4.0.