Click to See Complete Forum and Search --> : c# count integers in string


bubbisthedog
04-27-2009, 08:03 PM
I'm looking for the most efficient way to count the number of 0-9 integers in a string. I could write it, but it may be much less efficient than some of you peeps may know of. Any suggestions or solutions are welcomed.

Thanks,

bubbis

bubbisthedog
04-27-2009, 09:23 PM
For now I'm using

int Count;
string IntArray = "0123456789";
foreach (char Temp in CityString) {
if (IntArray.IndexOf(Temp) != -1) {
Count++;
}
}

Eh, it's a way to do it, but I'd love to see something more efficient if it exists...

namyaf
04-29-2009, 10:57 AM
You could do something like this:

Console.WriteLine(Regex.Replace("x1c2b3n4mdsd5h6k7h8k9h", "[^0-9]", "").Length.ToString());

Let me know how it works for you.

peace yo!