Click to See Complete Forum and Search --> : find substring in a string


lsy
February 29th, 2008, 12:51 AM
is string provide any function that allow to calculate there is how many substring inside a string??
example: string str = "how are you?"
is it possible to tell how many "o" inside the str?

dglienna
February 29th, 2008, 12:54 AM
Use the substring() command with a variable. when you find the string, move the variable past it, and then LOOP until you don't find it again.

that's actually pseudo-code that will solve the problem

Shuja Ali
February 29th, 2008, 01:30 AM
Another way would be to use Regular expressions. string str = "how are you?";
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("o");
int i = rx.Matches(str).Count;

nabeelisnabeel
February 29th, 2008, 04:03 AM
Another way would be to use Regular expressions. string str = "how are you?";
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("o");
int i = rx.Matches(str).Count;


Excellent.

lsy
February 29th, 2008, 04:48 AM
Another way would be to use Regular expressions. string str = "how are you?";
System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("o");
int i = rx.Matches(str).Count;

Thank you very much....