KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using Split string to get specific string count
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: August 23, 2019

The Split string command can be used to easily get the times a specific string appears in a text.
When the split string command is called, the text is split where the compared string is into elements of a collection. This means that the number of times the string was split is how many times the specific string appeared in the text. To get the number of times the string appeared just subtract one (1) to the number of elements in the resulting collection, easily done by looking at the length property.

Example:

C_TEXT($text_t)
C_COLLECTION($res_c)
C_LONGINT($count_l)

$text_t:="The Split string command returns a collection of strings, created by splitting stringToSplit into substrings at the boundaries specified by the separator parameter."

$res_c:=Split string($text_t;"string")
$count_l:=$res_c.length-1


The result of $count_l is 4 as underlined (which is only done in this Tech Tip for the sake of ease of seeing how the example works) with $res_c containing 5 elements.