5

Possible Duplicate:
How would you count occurences of a string within a string (C#)?

how do I get the count of the occurrences of '#' in a string ?

something like int RowFormat = drr[3].ToString("#").Length;

example string "grtkj####mfr "

RowFormat must return 4

and yes ^_^ .NET 3.5

Community
  • 1
  • 1
cnd
  • 32,616
  • 62
  • 183
  • 313

4 Answers4

26
int RowFormat = "grtkj####mfr".Count(ch => ch == '#');
Kamarey
  • 10,832
  • 7
  • 57
  • 70
  • 1
    Also, when posting a LINQ query, it is still sometimes a good idea to mention that it only works >3.5 unless the questions specifically mentions version. No better answer given the constraints though. – Nick Larsen Mar 16 '10 at 13:24
2

With LINQ (that's all the rage these days):

int RowFormat = drr[3].Count(x => x == '#');
Vilx-
  • 104,512
  • 87
  • 279
  • 422
1

Check this

"grtkj####mfr".Split(new char[]{'#'}).Length-1

hope that will help.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Asim Sajjad
  • 2,526
  • 10
  • 36
  • 73
0
int RowFormat = new Regex("#").Matches("grtkj####mfr").Count;
gingerbreadboy
  • 7,386
  • 5
  • 36
  • 62