比较与非英文字符的字符串?英文、字符串、与非、字符

2023-09-06 06:45:09 作者:你自月中来

我需要比较字符串在网站上搜索机制。我使用C#。我尝试了两种方式:

I need to compare strings for a search mechanism on a web site. I use C#. I tried two ways:

consultants.Where(x => 
    x.Description.ToLower().Contains(vm.Description.ToLower()));

consultants.Where(x => 
    Regex.IsMatch(x.Description, vm.Description, RegexOptions.IgnoreCase));

无论做工精细的所有英文字符。所以,如果我搜索,比如说,英语,那也没问题。但只要我尝试搜索包含非英语字符的字符串,这是行不通的。举例来说,如果我试图寻找它不返回任何单词språk(瑞典语言)。

Both work fine for all English characters. So if I search for, say, "english", that's no problem. But as soon as I try searching for a string that contains non-English characters, it doesn't work. For example, if I try searching for the word "språk" ("language" in Swedish) it returns nothing.

这是为什么,我该怎么解决呢?

Why is that, and how can I solve it?

推荐答案

使用

String.Equals(c, vm, StringComparison.OrdinalIgnoreCase)

c.IndexOf(vm, StringComparison.OrdinalIgnoreCase)

表示统一code,字节每字节,培养独立的比较。

Ordinal means Unicode, byte-per-byte, culture-independent comparison.