Ever Green

Ever Green

  • NA
  • 6
  • 0

How to count specific letterr in a string?

Nov 11 2009 4:20 PM
I Created a windows application in c#.net and I wanna count specific character in a string for example i wanna count how many "g" in "Debugging" string:
Someting is wrong with the Substring().I debugged it and i saw it is only check the first character of the string. Any help??

Code:

private void btnclick_Click(object sender, EventArgs e)
{
int lettercount = 0;
const string strtext = "Debugging";
string letter;

for (int i = 0; i < strtext.Length; i++)
{
letter = strtext.Substring(0,1);


if (letter == "g")
{
lettercount++;
}

}
textBox1.Text = "g appears" + lettercount + " " + "times";


}

Output:
"g appears 0 times"

Answers (4)