本文介绍了在Java中,stringByFoldingWithOptions:locale:等效于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找标准化标题列表的方法.标题被标准化以存储在数据库中,作为排序和查找键.规范化"是指很多事情,例如转换为小写字母,删除罗马重音符号或删除前面的"the","a"或"an".

I am looking for the way to normalise the list of titles. The title is normalized to be stored in a database as a sort and look up key. "Normalize" means many things such as converting to lowercase, removing the roman accent character, or removing preceding "the", "a" or "an".

在iOS或Mac中,NSString类具有stringByFoldingWithOptions:locale:方法来获取字符串的折叠版本.

In iOS or Mac, NSString class has stringByFoldingWithOptions:locale: method to get the folding version of string.

NSString类参考-stringByFoldingWithOptions:locale:

在Java中,java.uril.Collat​​or类对于进行比较似乎很有用,但似乎无法为此目的进行转换.

In Java, java.uril.Collator class seems to be useful for comparing, but there seems no way to convert for such purpose.

推荐答案

您可以使用 java.text.Normalizer ,几乎可以在Java中标准化Strings了.尽管 regex 也是一种以任何可能的方式操作字符串的强大方法.

You can use java.text.Normalizer which comes close to normalizing Strings in Java. Though regex are also a powerful way to manipulate the Strings in whichever way possible.

去除重音的示例:

String accented = "árvíztűrő tükörfúrógép";
String normalized = Normalizer.normalize(accented,  Normalizer.Form.NFD);
normalized = normalized.replaceAll("[^\\p{ASCII}]", "");

System.out.println(normalized);

输出:

arvizturo tukorfurogep

此处有更多说明: http://docs.oracle.com/javase/tutorial/i18n/text/normalizerapi.html

这篇关于在Java中,stringByFoldingWithOptions:locale:等效于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:44