contains()

The contains() function returns whether a target value contains a specified value.

Syntax

contains(TARGET, NEEDLE)

Parameters

TARGET
The value to search within. Accepts a string, array, or list.
NEEDLE
The value to check for within TARGET.

Description

The behavior of contains() depends on the types of TARGET and NEEDLE:

  • String: Returns true if TARGET contains NEEDLE as a substring, and false otherwise. Non-string values are converted using toString() before the check.
  • Array or list: Returns true if any element in the TARGET array or list equals NEEDLE, and false otherwise.

If TARGET or NEEDLE is null, the function returns false.

Error codes

N/A

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Check whether the User-Agent contains a crawler string

    table limit=5 WEB_APACHE_SAMPLE | eval result = contains(agent, "Googlebot") | fields agent, result
    
  2. Check whether the URI contains a specific path

    table limit=5 WEB_APACHE_SAMPLE | eval result = contains(uri, "/archives/") | fields uri, result
    
  3. NULL input

    json "{'val': null}" | eval result = contains(val, "foo")
    | # result: false
    

Compatibility

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