本文介绍了在Java 7中将重音和字符组合成一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Java代码,该代码返回一个结合了字符和重音符号的字符.合并的实际结果是一个字符串,而不是一个字符.下面是一个简单的方法来说明我正在尝试做的事情.谢谢

I am trying to write a java code that returns a single character combining both a character and an accent. The actual result of combining is a string and not one single character.The following is a simple method to illustrate what I am trying to do. Thank you

private char convert (char c)
{
 if (c == '\u0130')
 {
  return '\u0069 \u0307'; // If the return value is String I get i. 
}                         //I need small i double dot
else return c;
}

推荐答案

Normalizer可以根据需要分解/合成角色:

Normalizer can decompose/compose your character as you like:

String decomposed = Normalizer.normalize(String.valueOf('ï'), Form.NFD);

结果是两个字符(i,双点)

result are two character (i, double-dot)

String composed = Normalizer.normalize(decomposed, Form.NFC);

结果是一个字符(ï)

如果我对你的理解正确,你会寻找

If I understand you correctly you seek

return Normalizer.normalize("\u0069\u0307", Form.NFC).charAt(0);

对于双点,请使用\u0308.

这篇关于在Java 7中将重音和字符组合成一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 21:41