5
Reply

Correct way of searching a string for character in C#

Glenn Patton

Glenn Patton

Sep 3 2012 10:09 AM
1.1k
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 -=-=-= 

Answers (5)
Next Recommended Forum