A string is compared to special characters in C #

I have two lines of " CZSczs " - " ČŽŠčžš ", and I want to return true when I compare the lines. I tried string comparison, but this does not work.

+6
source share
2 answers

you can use

 int result string.Compare("CZSczs", "ČŽŠčžš", CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace); bool equal = result == 0; 

As indicated in this question, the accepted answer.

+2
source

You need to specify the culture:

 using System; public class Program { public static void Main() { string string1 = "CZSczs"; string string2 = "ČŽŠčžš"; if(String.Compare(string1, string2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0) { Console.WriteLine("same"); } else { Console.WriteLine("not same"); } } } 

See this working code: DotNetFiddle

0
source

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


All Articles