I have a string like this (could be longer)
string list = "ABCDGGWWWW1234567 ABCDGGWWWW1234568 ABCDGGWWWW1234569 ";
Now I need to check if the substring ABCDGGWWWW1234568
is in this string.
This I can do like the following
list.IndexOf("ABCDGGWWWW1234568 ") >= 0;
but, now I have the following problem,
if I would look for the substring ABCDXXWWWW1234568
then this must also result in true
To explain the problem, when I search than the 5th and 6th character can also be XX
In other words, ABCDGGWWWW1234568
= ABCDXXWWWW1234568
It is always both characters that will be XX
or the original value, not just one. Also the strings will always be 17 digits long. And they will always be uppercase.
So what would be the most efficient way to accomplish this ?
I was hoping that I could use wildcards like
list.IndexOf("ABCD__WWWW1234568 ") >= 0;
list.IndexOf("ABCD??WWWW1234568 ") >= 0;
but unfortunate that does not works, it never finds the index and always returns -1
EDIT
Thanks to @PanagiotisKanavos I have managed to get it working like this
chassis = chassis.Substring(0, 4) + @"\S{2}" + chassis.Substring(6, 11) + @"\s+";
bool result = Regex.IsMatch(list, chassis);
IndexOf
doesn't have wildcards. You need regular expressions for this. Use the Regex class, for example:
var found=Regex.IsMatch(list,"ABCD\S{2}WWWW1234568\s+");
.
matches any character. \S
matches any non-whitespace character. \S{2}
matches exactly two non-space characters. \s+
matches one or more whitespace characters