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"
Loading
Nilanka DharmadasaPosted Nov 12, 2009, 1:21 AM
Do this change.
Sam HobbsPosted Nov 12, 2009, 11:04 AM
Ever GreenPosted Nov 12, 2009, 10:51 AM
Thank you very much for help. It is working now.
I got the exact output what i was wanting.
Thanks a lot again.
Sam HobbsPosted Nov 11, 2009, 4:44 PM
letter = strtext.Substring(0,1);
Notice that Substring will always use zero for the index. You need to change that.
Also note that instead of strtext.Substring, you can use strtext[] to get each character. I would be more specific, but I think this is a class problem and the best way to help you is to help you to figure it out.