0

Shouldn't the following code return 4? It is returning -1

string sa = "soy mas grande";
Response.Write("CMP: "+sa.IndexOf("más", StringComparison.InvariantCulture)+"<br>");
Cornwell
  • 3,304
  • 7
  • 51
  • 84
  • From [here](http://msdn.microsoft.com/en-us/library/ms973919.aspx): "DON'T Use `StringComparison.InvariantCulture`-based string operations in most cases; one of the few exceptions would be persisting linguistically meaningful but culturally-agnostic data." It does not look like this is one of the exceptions. – Sergey Kalinichenko Nov 13 '13 at 14:18

2 Answers2

3

StringComparison.InvariantCulture won't strip accents, you may be looking for this:

System.Globalization.CultureInfo.InvariantCulture
      .CompareInfo.IndexOf(sa,
                           "más",
                           System.Globalization.CompareOptions.IgnoreNonSpace)
decPL
  • 5,384
  • 1
  • 26
  • 36
1

No; "más" is not "mas" so it isn't going to match.

Allan Elder
  • 4,052
  • 17
  • 19
  • I thought InvariantCulture would strip the accent – Cornwell Nov 13 '13 at 14:14
  • 1
    No; you need to look into removing accents from the string before comparing; see here http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net – Allan Elder Nov 13 '13 at 14:28