【问题标题】:What is the equivalent of stringByFoldingWithOptions:locale: in Java?什么是 stringByFoldingWithOptions:locale: 在 Java 中的等价物?
【发布时间】:2023-03-07 20:25:01
【问题描述】:

我正在寻找标准化标题列表的方法。标题被标准化以作为排序和查找键存储在数据库中。 “规范化”意味着很多东西,例如转换为小写、删除罗马重音字符或删除前面的“the”、“a”或“an”。

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

NSString Class Reference - stringByFoldingWithOptions:locale:

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

【问题讨论】:

    标签: java string normalize


    【解决方案1】:

    您可以使用java.text.Normalizer,它接近于规范化 Java 中的字符串。尽管regex 也是一种以任何可能的方式操纵字符串的强大方法。

    重音去除示例:

    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

    【讨论】:

    • 谢谢!这似乎是我正在寻找的。我会试试这个。
    猜你喜欢
    • 2021-10-18
    • 2010-12-07
    • 2013-03-12
    • 2014-12-06
    • 2011-06-05
    • 2010-11-22
    • 2011-04-13
    • 2012-09-14
    • 2011-03-23
    相关资源
    最近更新 更多