使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
strings.count_substrings
strings.count_substrings(string_to_search_in, substring_to_count)
说明
给定一个字符串和一个子字符串时,返回该子字符串在字符串中非重叠出现的次数(int64 类型)。
形参数据类型
STRING
,STRING
返回类型
INT
代码示例
本部分包含一些示例,用于计算子字符串在给定字符串中出现的次数。
示例 1
此示例使用非 null 字符串和非 null 单个子字符串字符。
strings.count_substrings("this`string`has`four`backticks", "`") = 4
示例 2
此示例使用非 null 字符串和长度大于 1 个字符的非 null 子字符串。
strings.count_substrings("str", "str") = 1
示例 3
此示例使用非 null 字符串和空子字符串。
strings.count_substrings("str", "") = 0
示例 4
此示例使用空字符串和长度大于 1 个字符的非 null 子字符串。
strings.count_substrings("", "str") = 0
示例 5
此示例使用空字符串和空子字符串。
strings.count_substrings("", "") = 0
示例 6
此示例使用一个非 null 字符串和一个非 null 子字符串,该子字符串的长度大于 1 个字符,且出现次数大于 1 次。
strings.count_substrings("fooABAbarABAbazABA", "AB") = 3
示例 7
此示例使用一个非 null 字符串和一个非 null 子字符串,该子字符串的长度大于 1 个字符,且出现次数大于 1 次。它突出了重叠子字符串出现的限制
strings.count_substrings("ABABABA", "ABA") = 2
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-29。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-29。"],[[["\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"]]