比较法语字符 Î 的问题

Problem comparing French character #206;(比较法语字符 的问题)
本文介绍了比较法语字符 Î 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

When comparing "Île" and "Ile", C# does not consider these to be to be the same.

    string.Equals("Île", "Ile", StringComparison.InvariantCultureIgnoreCase)

For all other accented characters I have come across the comparison works fine.

Is there another comparison function I should use?

解决方案

You are specifying to compare the strings using the Invariant culture's comparison rules. Evidently, in the invariant culture, the two strings are not considered equal.

You can compare them in a culture-specific manner using String.Compare and providing the culture for which you want to compare the strings:

if(String.Compare("Île", "Ile", new CultureInfo("fr-FR"), CompareOptions.None)==0)

Please note that in the french culture, those strings are also considered different. I included the example to show, that it is the culture that defines the sort rules. You might be able to find a culture that fits your requirements, or build a custom one with the needed compare rules, but that it probably not what you want.

For a good example of normalizing the string so there are no accents, have a look at this question. After normalizing the string, you would be able to compare them and consider them equal. This would probably be the easiest way to implement your requirement.

Edit

It is not just the I character that has this behaviour in the InvariantCulture, this statement also returns false:

String.Equals("Ilê", "Ile", StringComparison.InvariantCultureIgnoreCase)

The framework does the right thing - those characters are in fact different (has different meaning) in most cultures, and therefore they should not be considered the same.

这篇关于比较法语字符 Î 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)