Strange IndexOf of a string in c# which returns -1

Viewed 107

I have a string which looks like:

var a = @"DISC INFO:

blablabla";

And I want to detect if DISC INFO exists in that string.

I did in simple way:

var index = a.IndexOf("disc info", StringComparison.OrdinalIgnoreCase);

It returns me -1 ...

Why ? I expected to find it

The entire C# code: https://dotnetfiddle.net/DAgxau

3 Answers

There was a U+2002 : EN SPACE {nut} in between DISC and INFO.

I personally check this with notepad++, I'm not sure if you need any special settings to see the characters but this is how it looks:

enter image description here

So when using a normal space to match it won't work.

If you want to match unicode whitespace you can use Regex, credit to Marc Gravell.

Ok then, how to detect that strange character with space in .net c# ?

Probably your best bet is to use a regex instead of a simple match; the \s token matches unicode whitespace, not just literal space character (ASCII 32):

var match = Regex.Match(a, @"disc\sinfo", RegexOptions.IgnoreCase);

(you can look at match.Success and match.Index, etc)

Note, however, that it is not quite true that everything that looks and smells like a space is categorized as a space in the unicode tables. Plus: the unicode tables evolve over time, so it depends which unicode version Regex on your runtime and operating system is using. Mostly it'll work, though.

Problem with your original string. enter image description here

Related