string_count
Use the string_count() function to count the number of times a specified substring (pattern) appears within a given string.
Syntax
string_count (<string>, <pattern>)Parameters
string
string
Yes
The input string field or literal value in which you want to search.
pattern
string
Yes
The substring literal that you want to count.
Returns
The string_count() function returns an integer representing the number of occurrences found.
Usage notes
By default, XQL queries operate with case-sensitivity unless
config case_sensitive = falseis explicitly set. This also applies tostring_count().If the input
stringvalue isNULL, thestring_count()function will returnNULL.If the
patterndoes not appear in thestring, the function returns0.
Examples
Example 1: Counting occurrences of a specific character in a string field
Goal: Count the occurrences of the character 'o' in the event_description field.
XQL code:
Explanation: The query counts how many times the character 'o' appears in the event_description for each specified event.
Output:
101
"User login successful"
1
102
"File access attempt"
0
103
"Network connection established"
3
Example 2: Counting occurrences of a substring (word) in a string field
Goal: Count the occurrences of the word "access" in the raw_log_data field.
XQL code:
Explanation: The string_count() function correctly identifies and counts the single occurrence of "access" in event ID 102's raw_log_data.
Output:
101
"User Alice logged in from 192.168.1.10"
0
102
"Process cmd.exe attempted to access /etc/passwd"
1
Example 3: Counting delimiters in an IP address string
Goal: Count the number of dot (.) delimiters within ipv4_address strings.
XQL code:
Explanation: The query accurately counts the three dot separators in each IPv4 address string.
Output:
101
"192.168.1.10"
3
102
"10.0.0.5"
3
Example 4: Case-insensitive counting (default behavior)
Goal: Demonstrate string_count()'s default case-sensitive behavior by searching for "user" (lowercase) in a field containing "User" (uppercase).
XQL code:
Explanation: The pattern "user" (lowercase) does not successfully match "User" in the raw_log_data due to the case-sensitive behavior of XQL.
Output:
101
"User Alice logged in from 192.168.1.10"
0
Example 5: Case-sensitive counting (with config case_sensitive = false)
Goal: Explicitly set case sensitivity to false to show how it affects string_count() results when searching for "user" vs. "User".
XQL code:
Explanation: When config case_sensitive is false, string_count() does not distinguish between "User" and "user", returning 1 for both cases.
Output:
101
"User Alice logged in from 192.168.1.10"
1
1
Example 6: Counting a pattern not present in the string
Goal: Demonstrate the result when the specified pattern does not exist in the input string.
XQL code:
Explanation: As expected, string_count() returns 0 when the nonexistent_word pattern is not found in the event_description.
Output:
101
"User login successful"
0
Example 7: Handling NULL input string
Goal: Show how string_count() behaves when the input string field contains a NULL value.
XQL code:
Explanation: Consistent with XQL function behavior, if the input string (dst_domain for event ID 105) is NULL, the string_count() function returns NULL.
Output:
105
NULL
NULL
Related articles
Functions:
len,split,regextractDatasets:
xdr_data
Last updated
Was this helpful?
