比较单词与特殊字符(é、è、...)时忽略变音符号
我有一个包含一些带有变音符号的比利时城市的列表:(列日、Quiévrain、Franière 等),我想转换这些特殊字符以与包含大写相同名称但没有变音符号的列表进行比较( LIEGE、QUIEVRAIN、FRANIERE)
我首先尝试做的是使用大写:
LIEGE.contentEqual(Liège.toUpperCase())
但这不适合,因为 的大写列日
是LIÈGE
而不是LIEGE
。
我有一些复杂的想法,比如替换每个角色,但这听起来很愚蠢而且是一个漫长的过程。
关于如何以聪明的方式做到这一点有什么想法吗?
I have a list with some Belgian cities with diacritic characters: (Liège, Quiévrain, Franière, etc.) and I would like to transform these special characters to compare with a list containing the same names in upper case, but without the diacritical marks (LIEGE, QUIEVRAIN, FRANIERE)
What i first tried to do was to use the upper case:
LIEGE.contentEqual(Liège.toUpperCase())
but that doesn't fit because the Upper case of Liège
is LIÈGE
and not LIEGE
.
I have some complicated ideas like replacing each character, but that sounds stupid and a long process.
Any ideas on how to do this in a smart way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
从 Java 6 开始,您可以使用 java.text.Normalizer:
请注意,在 Java 5 中还有一个
sun.text.Normalizer
,但强烈建议不要使用它,因为它是 Sun 专有 API 的一部分,并且已在 Java 6 中删除。As of Java 6, you can use java.text.Normalizer:
Note that in Java 5 there is also a
sun.text.Normalizer
, but its use is strongly discouraged since it's part of Sun's proprietary API and has been removed in Java 6.在Java中查看这个方法
Check out this method in Java
这是迄今为止我发现的最简单的解决方案,它在我们的应用程序中完美运行。
但我不知道Normalizer在Android平台上是否可用。
This is the simplest solution I've found so far and it works perfectly in our applications.
But I don't know if the Normalizer is available on the Android platform.
如果您仍然需要 Android API 8 或更低版本(Android 2.2、Java 1.5),而您没有 Normalizer 类,这是我的代码,我认为修改比 Pentium10 答案更好:
If you still need that for Android API 8 or lower (Android 2.2, Java 1.5) where you don't have Normalizer class, here's my code, I think better to modify than Pentium10 answer:
Collator 类是一种很好的方法(请参阅相应的 javadoc)。这是一个显示如何使用它的单元测试:
编辑:
很遗憾我的回答没有满足您的需求;也许是因为我将其作为单元测试呈现?这对你来说可以吗?我个人觉得它更好,因为它短并且它使用SDK(不需要字符串替换)
希望这有帮助
The Collator class is a good way to do it (see corresponding javadoc). Here is a unit test that shows how to use it :
EDIT :
sorry to see my answer did not meet your needs ; maybe it's beause I've presented it as unit test ? Is this ok for you ? I personnaly find it better because it's short and it uses the SDK (no need for String replacement)
hope this helps
我不知道它是否在 Android 上可用,但在 JVM 上,您不应该在项目中重新实现它并重用已有的代码:只需使用 org.apache.commons.lang3.StringUtils#stripAccents
I don't know if it is avaible on Android but on the JVM, you should not reimplement it in your project and reuse already existing code: just use org.apache.commons.lang3.StringUtils#stripAccents
对于那些寻找干净的 java 解决方案的人,请使用 apache commons:
这将返回
For those looking for a clean java solution, use apache commons:
this will return
由于 Froyo 或以前的 Android 版本不支持 Normalizer 类,我已合并此和此< /a> (我都投了赞成票),并对其进行了优化,获得了一些辅助方法。方法unaccentify只是将变音符号转换为普通字符,而方法slugify为输入字符串生成一个slug。希望它对某人有用。这是源代码:
Since class Normalizer is not supported in Froyo or previous Android versions, I have combined this and this (which I both voted up), and optimized it, obtaining a couple of helper methods. Method unaccentify simply converts diacritic chars to plain chars, while method slugify generates a slug for the input string. Hope it can be useful to someone. Here is the source code: