Stay organized with collections
Save and categorize content based on your preferences.
strings.count_substrings
strings.count_substrings(string_to_search_in, substring_to_count)
Description
When given a string and a substring, returns an int64 of the count of non-overlapping occurrences of the substring within the string.
Param data types
STRING
, STRING
Return type
INT
Code samples
This section contains examples that calculate the number of times a substring appears in a given string.
Example 1
This example uses a non-null string and a non-null single substring character.
strings.count_substrings("this`string`has`four`backticks", "`") = 4
Example 2
This example uses a non-null string and a non-null substring greater than one character.
strings.count_substrings("str", "str") = 1
Example 3
This example uses a non-null string and an empty substring.
strings.count_substrings("str", "") = 0
Example 4
This example uses an empty string and a non-null substring greater than one character.
strings.count_substrings("", "str") = 0
Example 5
This example uses an empty string and an empty substring.
strings.count_substrings("", "") = 0
Example 6
This example uses a non-null string and a non-null substring that is greater than one character and greater than one occurrence.
strings.count_substrings("fooABAbarABAbazABA", "AB") = 3
Example 7
This example uses a non-null string and a non-null substring that is greater than one character and greater than one occurrence. It highlights the limitation with overlapping substring occurrences
strings.count_substrings("ABABABA", "ABA") = 2
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-14 UTC."],[[["\u003cp\u003eThe \u003ccode\u003estrings.count_substrings\u003c/code\u003e function calculates the number of non-overlapping instances a specified substring appears within a given string.\u003c/p\u003e\n"],["\u003cp\u003eIt takes two string inputs: the string to search within and the substring to count, and then returns an integer representing the count.\u003c/p\u003e\n"],["\u003cp\u003eAn empty substring or an empty string input, in any combination, will result in a returned count of 0.\u003c/p\u003e\n"],["\u003cp\u003eOverlapping occurrences of the substring are not counted; only distinct, non-overlapping instances are included in the count.\u003c/p\u003e\n"],["\u003cp\u003eThis function is supported within both Rules and Search.\u003c/p\u003e\n"]]],[],null,["### strings.count_substrings\n\nSupported in: \n[Rules](/chronicle/docs/detection/default-rules) [Search](/chronicle/docs/investigation/udm-search) \n\n strings.count_substrings(string_to_search_in, substring_to_count)\n\n#### Description\n\nWhen given a string and a substring, returns an int64 of the count of non-overlapping occurrences of the substring within the string.\n\n#### Param data types\n\n`STRING`, `STRING`\n\n#### Return type\n\n`INT`\n\n#### Code samples\n\nThis section contains examples that calculate the number of times a substring appears in a given string.\n\n##### Example 1\n\nThis example uses a non-null string and a non-null single substring character. \n\n strings.count_substrings(\"this`string`has`four`backticks\", \"`\") = 4\n\n##### Example 2\n\nThis example uses a non-null string and a non-null substring greater than one character. \n\n strings.count_substrings(\"str\", \"str\") = 1\n\n##### Example 3\n\nThis example uses a non-null string and an empty substring. \n\n strings.count_substrings(\"str\", \"\") = 0\n\n##### Example 4\n\nThis example uses an empty string and a non-null substring greater than one character. \n\n strings.count_substrings(\"\", \"str\") = 0\n\n##### Example 5\n\nThis example uses an empty string and an empty substring. \n\n strings.count_substrings(\"\", \"\") = 0\n\n##### Example 6\n\nThis example uses a non-null string and a non-null substring that is greater than one character and greater than one occurrence. \n\n strings.count_substrings(\"fooABAbarABAbazABA\", \"AB\") = 3\n\n##### Example 7\n\nThis example uses a non-null string and a non-null substring that is greater than one character and greater than one occurrence. It highlights the limitation with overlapping substring occurrences \n\n strings.count_substrings(\"ABABABA\", \"ABA\") = 2"]]