charcter count in a string

  • Thread starter Thread starter Kuups
  • Start date Start date
K

Kuups

Hi!

I have a question regarding the count if character within a string
like for example I have a string of

e.g.

123#123#

I would like to determine what is the code? of getting the # sign
which the answer is 2.


Hope you can help me on this.


Thanks and Regards..
 
Kuups said:
I have a question regarding the count if character within a string
like for example I have a string of

e.g.

123#123#

I would like to determine what is the code? of getting the # sign
which the answer is 2.

public static int CountCharacters (string data, char c)
{
int count=0;
foreach (char x in data)
{
if (x==c)
{
count++;
}
}
return count;
}
 
Hi Kuups,

string pattern = "12#3123#141#";
Console.WriteLine(pattern.Split('#').Length-1);

HTH,
Rakesh Rajan
 
Back
Top