views:

177

answers:

7

taken a string example as 550e8400-e29b-41d4-a716-446655440000 how can one count how many - chars are in such string?

I'm currently using:

int total = "550e8400-e29b-41d4-a716-446655440000".Split('-').Length + 1;

Is there any method that we don't need to add 1... like using the Count maybe?

All other methods such as

Contains IndexOf etc only return the first position and a boolean value, nothing returns how many were found.

what am I missing?

+4  A: 
int total = "550e8400-e29b-41d4-a716-446655440000".Count(c => c == '-');
LukeH
+13  A: 

You can use the LINQ method Enumerable.Count for this purpose (note that a string is an IEnumerable<char>):

int numberOfHyphens = text.Count(c => c == '-');

The argument is a Func<char, bool>, a predicate that specifies when an item is deemed to have 'passed' the filter.

This is (loosely speaking) equivalent to:

int numberOfHyphens = 0;

foreach (char c in text)
{
    if (c == '-') numberOfHyphens++;
}
Ani
+1 for explaining that this is part of LINQ, and why it works for this.
cHao
+1 Good answer!
Øyvind Bråthen
+2  A: 
int total = "550e8400-e29b-41d4-a716-446655440000".Count(c => c == '-')
Lee
+1  A: 

Try this:

string s = "550e8400-e29b-41d4-a716-446655440000";
int result = s.ToCharArray().Count( c => c == '-');
Øyvind Bråthen
Why bother calling ToCharArray first?
Jon Skeet
I also noticed that intellisense doesn't offered my Count(this) for a string, it only worked then converting to a char array. Maybe a bug in intellisense of vs2008?
codymanix
@codymanix - I believe it was a deliberate decision to hide extension methods for `IEnumerable<char>` on string instances. I can't remember the reasoning however.
Lee
@codymanix: I read somewhere (can't remember exactly where) that this was a deliberate design decision because they didn't want the full list of `IEnumerable<>` extensions popping up and cluttering intellisense whenever `string` was being used. A flawed decision, in my opinion.
LukeH
@codymanix - Here's a question on SO about it: http://stackoverflow.com/questions/345883/why-doesnt-vs-2008-display-extension-methods-in-intellisense-for-string-class
Lee
I see from the answers here that ToCharArray is not needed, so then I learned something as well :)
Øyvind Bråthen
+4  A: 
using System.Linq;

..

int total = "550e8400-e29b-41d4-a716-446655440000".Count(c => c == '-');
codymanix
+2  A: 

To find the number of '-' in a string, you are going to need to loop through the string and check each character, so the simplest thing to do is to just write a function that does that. Using Split actually takes more time because it creates arrays for no reason.

Also, it's confusing what you are trying to do, and it even looks like you got it wrong (you need to subtract 1).

Lou Franco
+1 for downwardly compatible
Tim Schmelter
+4  A: 

The most straight forward method is to simply loop throught the characters, as that is what any algorithm has to do some way or the other:

int total = 0;
foreach (char c in theString) {
  if (c == '-') total++;
}

You can use extension methods to do basically the same:

int total = theString.Count(c => c == '-');

Or:

int total = theString.Aggregate(0, (t,c) => c == '-' ? t + 1 : t)

Then there are interresting (but less efficient) tricks like removing the characters and compare the lengths:

int total = theString.Length - theString.Replace("-", String.Empty).Length;

Or using a regular expression to find all occurances of the character:

int total = Regex.Matches(theString, "-").Count;
Guffa
Now I wonder which of these variations have which benefits compared to each other and, if that matters, which one is faster or more efficient.
Cthulhu
+1 For a simple and readable solution
David Relihan
@Cthulhu: The simple loop is the most efficient, it's hard to improve on that. The `Count` version is resonably close in performance, and produces shorter code.
Guffa