String compare with special characters in C#

北城余情 提交于 2019-12-07 03:10:05

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/30051621/string-compare-with-special-characters-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!