java取代德语变音符号
我有以下问题。 我试图用java中的ä , ö , ü替换德语变音符号。 但它根本行不通。 这是我的代码:
private static String[][] UMLAUT_REPLACEMENTS = { { "Ä", "Ae" }, { "Ü", "Ue" }, { "Ö", "Oe" }, { "ä", "ae" }, { "ü", "ue" }, { "ö", "oe" }, { "ß", "ss" } }; public static String replaceUmlaute(String orig) { String result = orig; for (int i = 0; i < UMLAUT_REPLACEMENTS.length; i++) { result = result.replaceAll(UMLAUT_REPLACEMENTS[i][0], UMLAUT_REPLACEMENTS[i][1]); } return result; }
ä仍然是ä等等。 我不知道这个问题是否与编码有关,但String包含我想要替换的确切字符。
先感谢您
首先,Unicode中存在一个小问题:
-
ä
可能是一个代码点SMALL_LETTER_A_WITH_UMLAUT或两个代码点:SMALL_LETTER_A后跟COMBINING_DIACRITICAL_MARK_UMLAUT。
为此,可以规范化 Unicode文本。
s = Normalizer.normalize(s, Normalizer.Form.NFKC);
C
意味着撰写,并将产生紧凑版本。
第二个更棘手的问题是,编辑器中java源代码的编码必须与用于javac -encoding ...
编译器的javac -encoding ...
相同。
您可以通过使用(测试方式)u-escaping来测试编码是否正确:
"\u00E4" // instead of ä
我的猜测是,这可能是问题所在。 国际规范似乎已经成为使用UTF-8进行Java源代码和编译。
此外,你可以使用
result = result.replace(UMLAUT_REPLACEMENTS[i][0], UMLAUT_REPLACEMENTS[i][1]);
没有正则表达式替换,更快。
您的代码看起来很好, replaceAll()
应该按预期工作。
试试这个,如果你还想保留资本化(例如ÜBUNG
将成为UEBUNG
,而不是UeBUNG
):
private static String replaceUmlaut(String input) { //replace all lower Umlauts String output = input.replace("ü", "ue") .replace("ö", "oe") .replace("ä", "ae") .replace("ß", "ss"); //first replace all capital umlaute in a non-capitalized context (eg Übung) output = output.replace("Ü(?=[a-zäöüß ])", "Ue") .replace("Ö(?=[a-zäöüß ])", "Oe") .replace("Ä(?=[a-zäöüß ])", "Ae"); //now replace all the other capital umlaute output = output.replace("Ü", "UE") .replace("Ö", "OE") .replace("Ä", "AE"); return output; }
资源
这最终对我有用:
private static String[][] UMLAUT_REPLACEMENTS = { { new String("Ä"), "Ae" }, { new String("Ü"), "Ue" }, { new String("Ö"), "Oe" }, { new String("ä"), "ae" }, { new String("ü"), "ue" }, { new String("ö"), "oe" }, { new String("ß"), "ss" } }; public static String replaceUmlaute(String orig) { String result = orig; for (int i = 0; i < UMLAUT_REPLACEMENTS.length; i++) { result = result.replace(UMLAUT_REPLACEMENTS[i][0], UMLAUT_REPLACEMENTS[i][1]); } return result; }
感谢您的所有答案和帮助。 它最终是nafas(带有新String)和Joop Eggen(正确的replace-Statement)的混合体。 你得到了我的upvote非常感谢!
编码编码编码….
不同的输入源可能会导致String编码的复杂化。 例如,一个可以具有UTF-8
编码,而另一个可以是ISO
有些人建议代码适用于它们,因此,最有可能的是你的字符串在处理时具有不同的编码。 (不同的编码会产生不同的字节数组,因此无法替换…)
要从根本上解决您的问题,您必须确保每个源使用完全相同的编码。
试试这个练习,希望能帮助你解决问题:
1 – 尝试这个:
System.out.println(Arrays.asList("Ä".getBytes()); //1 and 2 should have same results System.out.println(Arrays.asList(new String("Ä","UTF-8").getBytes()); //1 and 2 should have same results System.out.println(Arrays.asList(new String("Ä","UTF-32").getBytes()); //should have a different results from one and two System.out.println(Arrays.asList(orig.getBytes()); //look for representation and search for pattenr of numbers (this bit is the hard bit I guess). System.out.println(Arrays.asList(new String(orig,"UTF-32").getBytes()); //look for representation and search for pattenr of numbers (this bit is the hard bit I guess).
下一步是看看如何形成orgi
字符串。 例如,如果您是从Web收到的,请确保您的POST和GET方法使用您的首选编码
编辑1:
试试这个:
{ { new String("Ä".getBytes(),"UTF-8"), "Ae" }, ... };
如果这个不起作用试试这个:
byte[] bytes = {-61,-124}; //byte representation of Ä in utf-8 String Ae = new String(bytes,"UTF-8"); { { Ae, "Ae" }, ... }; //and do for the rest
我刚尝试运行它,运行正常。
如果你没有使用正则表达式,那么我会使用string.replace
而不是string.replaceAll
因为它比后者略快。 它们之间的区别主要在于replaceAll可以处理正则表达式。
编辑:刚刚注意到评论中的人在我面前说了同样的话,所以如果你已经阅读过,那么你几乎可以忽略我所说的内容,正如所述,代码中的其他地方存在问题,因为该代码片段按预期工作。
我尝试时工作正常,所以它必须是一个编码问题。
检查您的系统编码。 您可能希望在您的javac
编译器命令行中添加-encoding UTF-8
。
-encoding encoding Set the source file encoding name, such as EUC-JP and UTF-8. If -encoding is not specified, the platform default converter is used.
我不得不修改user1438038的答案:
private static String replaceUmlaute(String output) { String newString = output.replace("\u00fc", "ue") .replace("\u00f6", "oe") .replace("\u00e4", "ae") .replace("\u00df", "ss") .replaceAll("\u00dc(?=[az\u00e4\u00f6\u00fc\u00df ])", "Ue") .replaceAll("\u00d6(?=[az\u00e4\u00f6\u00fc\u00df ])", "Oe") .replaceAll("\u00c4(?=[az\u00e4\u00f6\u00fc\u00df ])", "Ae") .replace("\u00dc", "UE") .replace("\u00d6", "OE") .replace("\u00c4", "AE"); return newString; }
这应该适用于任何目标平台(我在Windows上的tomcat上有问题)。