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
trueifTARGETcontainsNEEDLEas a substring, andfalseotherwise. Non-string values are converted usingtoString()before the check. - Array or list: Returns
trueif any element in theTARGETarray or list equalsNEEDLE, andfalseotherwise.
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.
-
Check whether the User-Agent contains a crawler string
table limit=5 WEB_APACHE_SAMPLE | eval result = contains(agent, "Googlebot") | fields agent, result -
Check whether the URI contains a specific path
table limit=5 WEB_APACHE_SAMPLE | eval result = contains(uri, "/archives/") | fields uri, result -
NULL input
json "{'val': null}" | eval result = contains(val, "foo") | # result: false
Compatibility
The contains() function is available since before Sonar 4.0.