Click to See Complete Forum and Search --> : Get a count of multiple substrings?


panuvin
10-13-2006, 05:42 PM
Hello all,

I'm trying to figure out a way to go through a string and count the number of times a certain string occurs. I'm not sure if there's a built-in function to do this, but I wrote the following non-working function to do so. Can anyone tell me what's wrong with it or how I can accurately get a count of substrings?


string temp = htmlSource;
int chargeCt = 0;

while (temp.Contains("Charge Number") == true)
{
temp.Replace("Charge Number", "Charge #");
chargeCt++;
}


Thanks in advance!

Panuvin

sirpelidor
10-13-2006, 09:37 PM
take a look at the string class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemStringMethodsTopic.asp) use the method "indexof" instead of "contains"


while (temp.indexOf("Charge Number") != -1){
//code
}