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
  • 30,795
  • 60
  • 175
  • 303

4 Answers4

26
int RowFormat = "grtkj####mfr".Count(ch => ch == '#');
Kamarey
  • 10,528
  • 7
  • 56
  • 69
  • 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-
  • 101,209
  • 85
  • 267
  • 409
1

Check this

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

hope that will help.

Bill the Lizard
  • 386,424
  • 207
  • 554
  • 861
Asim Sajjad
  • 2,476
  • 10
  • 36
  • 71
0
int RowFormat = new Regex("#").Matches("grtkj####mfr").Count;
gingerbreadboy
  • 7,168
  • 5
  • 32
  • 60