So basically what you want is to scan your sequence for the characters '<', '>', '=' and '&', and if any of them found remember the index and the found character, if '<' or '>' is found you want to know if '=' is after it, and if so, the next search should start after the '='.
Note that you didn't specify what you want with &= or ==.
Whenever you have to scan strings for some syntax, it is always wise to at least consider the use of regular expressions.
According to the specification above you want a regular expression that matches if you find any of the following:
- '<='
- '>='
- '='
- '&'
- '<' followed by something else than '='
- '>' followed by something else than '='
Code would be simple:
using System.Text.RegularExpressions;
string expression = ...;
var regex = new RegularExpression("&|<=|>=|[<>][^=]");
var matches = regex.Matches(expression);
Object matches is an array of Match objects. Every match object has properties Index, Length and Value; exactly the properties you want.
foreach (var match in matches)
{
Console.WriteLine($"Match {match.Value} found"
+ " at index {match.Index} with length {match.Length}");
}
The vertical bar | in the regular expression means an OR; the [ ] means any of the characters between the brackets,; the [^ ] means NOT any of the characters between the brackets.
So a match is found if either & or <= or >= or any character in <> which is not followed by =.
If you also want to find &= and ==, then your reguilar expression would be even easier:
- find any <>&= that is followed by =
- or find any <>&= that is not followed by =
Code:
var regex = new Regex("[<>&=]|[<>&=][^=]");
A good online regex tester where you can check your regular expression can be found here. This shows also which matches are found and a description of the syntax of regular expressions.