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

I have a string which looks like:

var a = @"DISC INFO:


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:

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:

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.

Professional C# 6 and .NET Core 1.0, { WriteLine($"{r:A}"); } Starting the program with this LINQ query returns Niki Because Mario Andretti is positioned within an index that is odd, he is not in the passing the string class to the generic parameter returns only the strings from the​  The method returns -1 if the character or string is not found. This method can be overloaded by passing different parameters to it. String.IndexOf(char x) String.IndexOf(char x, int start1) String.IndexOf(char x, int start1, int start2) String.IndexOf(string s1) String.IndexOf(string s1, int start1) String.IndexOf(string s1, int start1, int start2)

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.

How to ignore the case sensitivity in List<string> while calculating of , You could use FindIndex: int ix = ExtPos.FindIndex(x => ".DAT".Equals(x, StringComparison.CurrentCultureIgnoreCase));. Or you could use the  IndexOf seems like a fairly straight forward method, it looks for the index of a character (or set of characters - aka. a string) and tells you the index of where it appears. If you look for the index of something that isn't there it usually returns -1. (Usually because PHP and their strpos method return false instead)

Problem with your original string.

Professional C# 2008, Because Mario Andretti is positioned within an index that is odd, he is not in the passing the string class to the generic parameter returns only the strings from  IndexOf(String, Int32, Int32) Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions.

Programming C# 4.0: Building Windows, Web, and RIA Applications , This takes the form of a slightly unusual-looking property, as shown in A custom indexer class Indexable { public string this[int index] { get { return "Item " +​  IndexOf() is the method that returns the integer value of the position of the first appearance of the character or a string in the selected string.If that value doesn’t exist, the method will return -1.

String#LastIndexOf - C# / C Sharp, C# / C Sharp Forums on Bytes. I encounter a strange behaviour of String.​LastIndexOf when specifying then I can file one for you in return. :) Gibt den NULL-basierten Index des ersten Vorkommens eines angegebenen Unicode-Zeichens oder einer angegebenen Unicode-Zeichenfolge in dieser Instanz an.Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. Die Methode gibt -1 zurück, wenn das Zeichen oder die Zeichenfolge in dieser Instanz nicht gefunden wird.The method returns

IndexOf problem, Visual C# Language get the following code snippet to work, but for some odd reason it's not foreach (string s in arrLst2) { count = CultureInfo. The expected end result should be "test-company", but the IndexOf returns 0. We use IndexOf to see if a string contains a word. We test the string for a substring "dog." We test the result of IndexOf against the special constant -1. Example: IndexOf returns the location of the string "dog." It is not equal to -1. So the line is written to the console window. Note: Usually we want to know the exact result of IndexOf. We

  • index -1 means it wasn't found. I'm looking at the fiddle now
  • I know what means ... I expected to find it...since it appear at beginning of string
  • I found the issue. the space in between disc and info isn't an actual space! If you remove that "space" and put a normal one there your program will work
  • @EpicKip beat me to it; you should post that as an answer, IMO
  • You can also just copy/paste the text into my little tool at
  • A different approach would be a = new string(a.ToCharArray().Select(c => char.GetUnicodeCategory(c) == UnicodeCategory.SpaceSeparator ? ' ' : c).ToArray()); where we only replace those that have the space separator category, I guess that doesn't include newlines and tabs and such, as they are control.
  • Ok then, how to detect that strange character with space in .net c# ?
  • You will have to special case it, if you expect this to happen in the future. You can replace all characters in the string that have unicode category Space with an actual space, something like a = new string(a.ToCharArray().Select(c => char.GetUnicodeCategory(c) == UnicodeCategory.SpaceSeparator ? ' ' : c).ToArray()); You can probably write a more optimal version of this using a StringBuilder.
  • @LasseVågsætherKarlsen or... 1 line with regex?
  • Depends on what you want :) Personally I would use the space separator approach, though I would probably write it differently, to avoid replacing newlines and such.
  • I guess you could do something like this in your regex: \p{Zs} instead of \s to limit to only space characters.