.net字符串与排序规则的比较 [英] .net string comparison with collation

查看:121
本文介绍了.net字符串与排序规则的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个不同的字符串(XXÈXXE).有没有什么方法可以使用归类比较它们(在这种情况下,这将是UTF8 general CI-我需要它们相等)?我见过一些涉及MSSQL或SQLLite的示例-但这会给我的项目增加不必要的依赖.所以,我的问题是-有什么办法可以在纯.net(尤其是C#)中做到这一点?

更新:

让我们以任何不错的SQL引擎为例.您可以创建一个表,也可以选择该表的排序规则.在我们的例子中,XXÈXXE将存储在表中,它们将具有不同的二进制表示形式(取决于编码),但是当您搜索XXE时,它们也会与XXÈ匹配.

我的情况非常相似.我有一个带有一些字符串的文本文件(UTF8).我想在屏幕上显示值(排序-排序规则再次出现,相对重要),并且我想让用户搜索值.用于搜索的排序规则将是一个选项.

解决方案

您可以使用

参考: http://www.blackwasp.co.uk/RemoveDiacritics.aspx

I have 2 different strings (XXÈ and XXE). Is there any way to compare them using a collation (for this case, it would be UTF8 general CI - I need them to be equal)? I've seen few examples involving MSSQL or SQLLite - but this would add an unnecessary dependency to my project. So, my question is - is there any way to do this in pure .net (especially c#)?

Update:

Let's take any decent SQL engine as an example. You can create a table and you can select the collation for the table. In our case, XXÈ and XXE will be stored in the table, they will have different binary representations (depending on the encoding), but when you search for XXE, it will match also XXÈ.

My case is pretty much similar. I have a text file with some strings in it (UTF8). I want to display the values on screen (sorted - where the collation is again, relatively important) and I want to let the user search for values. The collation used for search will be an option.

解决方案

You could use String.Normalize and a little bit LINQ-power:

string initial = "XXÈ";
string normal = initial.Normalize(NormalizationForm.FormD);

var withoutDiacritics = normal.Where(
    c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark);
string final = new string(withoutDiacritics.ToArray());
bool equals = "XXE".Equals(final); // true

Reference: http://www.blackwasp.co.uk/RemoveDiacritics.aspx

这篇关于.net字符串与排序规则的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆