Hi All,
I need to search a string for an '=' sign,
string Text = "";
int Gotit = 0;
Text = richTextBox2.Text;
Gotit = Text.IndexOf("=",7,1);
//MessageBox.Show(richTextBox2.Text);
if (Gotit >1)
{
MessageBox.Show("Got It!");
}
The bit of code above will find the equals at a set location (where it happens to be in the string), What I need to is search to see if Text has more than one '=' in it. My thinking was use the above code to find one '=' something like the below
if (Gotit >1)
{
//MessageBox.Show("Got It!");
for (int a = 7; a < Text.Length; a++)
{
Gotit2 = Text.IndexOf("=", a, 1);
MessageBox.Show("Hello");
break;
}
bearing in mind the following Text.Length will always be greater than 7 the location of the first '=', this however seems to detect the first '=' (and only one if there is only one) in Text. HELP!
Glenn
=-=- Also why is a++ unreachable, is due to the compiler seeing Text being equal to "" at compile time but when it is used Text.Length does not equal 0 -=-=-=
Loading
VulpesPosted Sep 3, 2012, 10:53 AM
int count = Text.Split('=').Length - 1;
Personally, I've never been keen on this approach as the Split method creates an array which then needs to be garbage collected.
The LINQ approach, also one line, would be:
int count = Text.Count(c => c == '=');
That also does some fancy footwork behind the scenes but is much loved by many developers nowadays :)
Glenn PattonPosted Sep 3, 2012, 11:06 AM
Glenn PattonPosted Sep 3, 2012, 11:06 AM
Glenn PattonPosted Sep 3, 2012, 11:05 AM
VulpesPosted Sep 3, 2012, 10:30 AM