StringOccurrenceCount

Counts the number of times a sub-string occurs inside another string.

Syntax

StringOccurrenceCount( StringToSearch, SubstringToFind );

Parameters

Parameter Name Type Description Optional? Default Value
StringToSearch String The string to search. No n/a
SubstringToFind String The sub-string you want to find and count inside StringToSearch. No n/a

Output

The number of occurrences of the SubstringToFind in StringToSearch.

Use Case

A genealogy mapping website lets users upload details of their family, which are initially compiled as a comma-separated string list that is stored in the SessionUserFamilyList variable.

The relationship type of each family member is represented with a distinct code, such as rtBrother for brothers, and so on. The data in SessionUserFamilyList can be analysed by a series of StringOccurrenceCount statements, each of which counts the instances of a particular relationship code and assigns the result to a variable. Examples of this are shown below:

FamilySisterCount:= StringOccurrenceCount( SessionUserFamilyList, "rtSister" );
FamilyUncleCount:= StringOccurrenceCount( SessionUserFamilyList, "rtUncle" );

In one instance, a user supplies details that result in the following string list, where the first value in each item is the family member's given name, and the second is their relationship type:

"Bob,rtUncle|Dan,rtFather|Roseanne,rtMother|David,rtUncle|Kimmy,rtSister"

If we were to use the two StringOccurrenceCount statements we previously mentioned, a value of 2 would be assigned to the FamilyUncleCount variable (For Bob and David), and a value of 1 would be assigned to the FamilySisterCount variable (For Kimmy).

Next, we use the following expression on the same family data:

FamilyBrotherCount:= StringOccurrenceCount( SessionUserFamilyList, "rtBrother" );

As a result, a value of 0 is assigned to the FamilyBrotherCount variable, because there are no instances of rtBrother in the main string.