使用规范分解规范化您的字符串:
private static final Pattern NonLATIN = Pattern.compile("[^\w-]"); private static final Pattern WHITESPACE = Pattern.compile("[\s]"); public static String toSlug(String input) { String nowhitespace = WHITESPACE.matcher(input).replaceAll("-"); String normalized = Normalizer.normalize(nowhitespace, Form.NFD); String slug = NONLATIN.matcher(normalized).replaceAll(""); return slug.toLowerCase(Locale.ENGLISH); }但是,这仍然是一个相当幼稚的过程。对于s-sharp(德语中使用的ß)或任何非基于拉丁语的字母(希腊语,西里尔字母,CJK等),它都不会做任何事情。
更改字符串大小写时请多加注意。 大写和小写形式取决于字母。在土耳其语中,U + 0069( i )的大写字母是U + 0130( İ),而不是U + 0049( I),因此,如果String.toLowerCase()
在土耳其语区域设置下使用,则可能会在字符串中引入非latin1字符。



