Newbie! Search a string for a specific character?
-
Hi! This dont seem to work, any ideas? (Im sure you have lots of ideas, but remember Im a newbie) label1.Text += "."; string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r > 0) { MessageBox.Show("bla"); } so, r is never above 0 despite that the string contains a "."
Newbie untill I die! :-)
-
Hi! This dont seem to work, any ideas? (Im sure you have lots of ideas, but remember Im a newbie) label1.Text += "."; string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r > 0) { MessageBox.Show("bla"); } so, r is never above 0 despite that the string contains a "."
Newbie untill I die! :-)
IndexOf returns a 0 based index. "." is not in position 1, it is in position 0. If nothing is found -1 is returned. Your code should be if( r != -1 )
My pointless rants meragussa.blogspot.com[^]
-
IndexOf returns a 0 based index. "." is not in position 1, it is in position 0. If nothing is found -1 is returned. Your code should be if( r != -1 )
My pointless rants meragussa.blogspot.com[^]
hi, and thanks for your fast answer. Im trying to program a calculator and you should not be able to hit "." multiple times in a row. So if I take this code: string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r != -1) { label1.Text += "."; MessageBox.Show("bla"); } And paste in under, for example, the button 9, I still get the msgbox! That should not happend, shouild it? Because now, there are no "." in the string...or Im missing someting here?
Newbie untill I die! :-)
-
hi, and thanks for your fast answer. Im trying to program a calculator and you should not be able to hit "." multiple times in a row. So if I take this code: string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r != -1) { label1.Text += "."; MessageBox.Show("bla"); } And paste in under, for example, the button 9, I still get the msgbox! That should not happend, shouild it? Because now, there are no "." in the string...or Im missing someting here?
Newbie untill I die! :-)
-
hi, and thanks for your fast answer. Im trying to program a calculator and you should not be able to hit "." multiple times in a row. So if I take this code: string k = label1.Text; int r; r = label1.Text.IndexOf(k); if (r != -1) { label1.Text += "."; MessageBox.Show("bla"); } And paste in under, for example, the button 9, I still get the msgbox! That should not happend, shouild it? Because now, there are no "." in the string...or Im missing someting here?
Newbie untill I die! :-)
hristo1977 wrote:
Im trying to program a calculator and you should not be able to hit "."
Here's a simple way to determine if the period character exists multiple times in a string:
string text = label1.Text;
int periodCount = 0;
foreach(char character in text)
{
if(character == '.')
{
periodCount++;
}
}if(periodCount > 1)
{
// There are multiple period characters in the string.
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: For Christians: The Significance of Yom Teruah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
hristo1977 wrote:
Im trying to program a calculator and you should not be able to hit "."
Here's a simple way to determine if the period character exists multiple times in a string:
string text = label1.Text;
int periodCount = 0;
foreach(char character in text)
{
if(character == '.')
{
periodCount++;
}
}if(periodCount > 1)
{
// There are multiple period characters in the string.
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: For Christians: The Significance of Yom Teruah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Thanks a lot, Himango!
Newbie untill I die! :-)