我想存储String
的标准化版本,以便能够对其进行like 'xxxx%'
次搜索。
我正在寻找一个高效且友好的用户搜索。
我最初的想法是转换为小写,删除非字母字符,重音符号和冗余空格,但不知道是否有一个已经研究和实施并准备就绪的好文章。
PD:该字符串将包含位置名称。
答案 0 :(得分:1)
最后,我以自定义解决方案结束。它可能更有效但它对我来说表现很好:
public static normalize(String string) {
string = string.toLowerCase();
//Remove/change all special characters --> àaç is converted to aac
String temp = Normalizer.normalize(string, Normalizer.Form.NFD);
string = pattern.matcher(temp).replaceAll("");
//Remove extra spaces
string = StringUtils.normalizeSpace(string);
}
StringUtils.normalizeSpace
来自Commons Lang。如果您不想引入依赖项,可以从函数中轻松get the code:
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("(?: \\s|[\\s&&[^ ]])\\s*");
public static String normalizeSpace(final String str) {
if (str == null) {
return null;
}
return WHITESPACE_PATTERN.matcher(trim(str)).replaceAll(SPACE);
}
答案 1 :(得分:0)
您可以使用搜索API吗?它默认提供文本规范化,并且设计用于最终用户查询(即,如果正确使用索引/命名空间,则可以将用户查询传递到Search API而无需验证。)