Jonathan.
Jonathan.

Reputation: 55604

How many times a string occurs in another string (textbox) VB.NET

How can I get how many times a string occurs in a textbox?

I thought the find function would return the number of times found but it seems to return the location of it.

Upvotes: 4

Views: 8748

Answers (2)

SLaks
SLaks

Reputation: 888185

You can call Split, like this:

(" " + textBox1.Text + " ").Split(New String() { inputString }, StringSplitOptions.None);

Alternatively, you can keep calling IndexOf with the startIndex equal to the previous call's return value + 1 until it returns -1.

Upvotes: 0

Aviad P.
Aviad P.

Reputation: 32670

Regex.Matches(textBox1.Text, Regex.Escape(inputString)).Count

Upvotes: 12

Related Questions