Why don't String.IndexOf and String.Contains agree to provide Arabic text?

I want to know if I found an error in the .NET Framework or if something does not understand. After running this piece of code:

var text = "ู…ุจุงุฑูƒู ูˆุจุนุถ ุฃูƒุซุฑ ู…ู† ู†ุต"; var word = "ู…ุจุงุฑูƒ"; bool exist = text.Contains(word); int index = text.IndexOf(word); 

The results are "exist = true" and "index = -1"

How can it be?

+6
source share
1 answer

Contains insensitive to culture:

This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

IndexOf culture sensitive:

This method searches for words (case-sensitive and culture-sensitive) using the current culture.

This is the difference. If you use

 int index = text.IndexOf(word, StringComparison.Ordinal); 

then you get index 0 instead of -1 (so it matches Contains ).

No Culture Sensitive Overload Contains ; I donโ€™t understand if IndexOf can be reliably used for this, but the CompareInfo class provides a few more options. (I really don't know much about the details of cultural comparisons, especially with the text RTL. I just know it's complicated!)

+9
source

Source: https://habr.com/ru/post/953602/


All Articles