Summary

Counts the occurrences of a string within a text.

Code (C#)

string text = Input.Text;  // The text to search in
string searchText = "ipsum";  // The text to search for

// Loop through all instances of the search text
int count = 0;
int index = 0;
while ((index = text.IndexOf(searchText, index)) != -1)
{
    index += searchText.Length;
    count++;
}

// Write results to the "Output" editor
Output.Text = string.Format("{0} occurences of '{1}' found in text.",
    count, searchText); 
Input Type: Plain Text, Output Type: Plain Text

Download Project File

File: Count_string_occurrences.dvp (2.83 KB)

To open this file, DataVoila must be installed on your computer. If this is not yet the case, please click here to download the free demo version.