简体   繁体   English

字符串与C#中的特殊字符进行比较

[英]String compare with special characters in C#

I have two strings " CZSczs " - " ČŽŠčžš " and I want to return true when I compare the strings. 我有两个字符串“ CZSczs ” - “ ČŽŠčžš ”,我想在比较字符串时返回true。 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 请参阅以下工作代码: DotNetFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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