简体   繁体   中英

String compare with special characters in C#

I have two strings " CZSczs " - " ČŽŠčžš " and I want to return true when I compare the strings. I tried with string comparison but it doesn't work.

You can use

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

As pointed out in this question 's accepted answer.

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 on : DotNetFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM