string manipulation

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

Is there a way to search a string for the number of times
a certain character shows up without having to write my
own function?
string var = "111-222-33.33-2"
var.countchar(var) = 3
 
Kevin said:
Is there a way to search a string for the number of times
a certain character shows up without having to write my
own function?
string var = "111-222-33.33-2"
var.countchar(var) = 3

Your example doesn't make a lot of sense - what is it counting there?
You haven't specified a character.

There's no function that I know of which does that - but then it's not
something that needs to be done very often. It's extremely easy to do
though:

int Count (string haystack, char needle)
{
int count=0;
foreach (char c in haystack)
{
if (c==needle)
count++;
}
return count;
}
 
Hi,
parse the string with a regex put the matches in a MatchCollection and take
the count Property of the MatchCollection

--
Mit freundlichen Gruessen - Regards

Ralph Gerbig
(e-mail address removed)
www.ralphgerbig.de.vu
 
Ralph Gerbig said:
parse the string with a regex put the matches in a MatchCollection and take
the count Property of the MatchCollection

That sounds like an awful lot of work though for something which is
pretty simple. I personally think that regular expressions are rather
overused. They're incredibly powerful, but sometimes a nail is just a
nail, and doesn't need a powertool, just a hammer :)
 
Ralph,

Can you use Jon's needle and haystack example and let's see what approach is
preferable?


int Count (string haystack, char needle)
{
int count=0;
foreach (char c in haystack)
{
if (c==needle)
count++;
}
return count;
}



Fritz
 
It's an interesting .NET performance question. Just to see how fast each
technique was I wrote a quick test to compare three methods:

Method 1 uses a foreach loop to retrieve each character in the string.

Method 2 uses ToCharArray() to get all the characters in the string, then
uses a for loop (not foreach) to go through each character in the array.

Method 3 uses a Regex and gets the Regex.Matches.Count property.

With a test string that contains 83 characters (arbitrary) it turns out that
method 2 is about five or six times faster than method 1, and about a
hundred times faster than method 3.

With a test string that contains 1000 characters method 2 gets even better,
outperforming method 1 by 7.5 to 1 and method 3 by 150 to 1.

Modifying method 2 to use foreach instead of for didn't measureably change
the performance.

Here's method 2 code:

int CountChar(string s, char ch)
{
int count = 0;
char[] a = s.ToCharArray();
foreach (char c in a)
if (c == ch)
count++;
return count;
}
 
Bret Mulvey said:
It's an interesting .NET performance question. Just to see how fast each
technique was I wrote a quick test to compare three methods:

Method 1 uses a foreach loop to retrieve each character in the string.

Method 2 uses ToCharArray() to get all the characters in the string, then
uses a for loop (not foreach) to go through each character in the array.

Method 3 uses a Regex and gets the Regex.Matches.Count property.

With a test string that contains 83 characters (arbitrary) it turns out that
method 2 is about five or six times faster than method 1, and about a
hundred times faster than method 3.

Out of interest, which version of .NET are you using? I believe that
foreach iteration over a string is *much* better with 1.1 than with
1.0.
 
Ralph Gerbig said:
It would be three lines :)

But those three lines would in turn do an awful lot more work - I
suspect you'd find it's very inefficient, and those lines themselves
are *relatively* complicated (that's relative to just counting the
occurrences of the character directly).
 
It's 1.0. I'll try it with 1.1 to compare.

Jon Skeet said:
Out of interest, which version of .NET are you using? I believe that
foreach iteration over a string is *much* better with 1.1 than with
1.0.
 
Jon,
I would have used String.IndexOf to find each occurrence of the character,
then I saw your foreach example and thought "Doh! that's simpler".

Something like:

int Count(string haystack, char needle)
{
int count = 0;
for (int index = haystack.IndexOf(needle); index > 0; index =
haystack.IndexOf(needle, ++index))
count++;
return count;
}

Which I would not expect to be more efficient then the foreach loop.

Just another thought on the subject,

Jay
 
Back
Top