本文介绍了如何仅删除字符串中大写字母的变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要删除字符串中大写字母的变音符号.示例:ÉlectroniqueCaméras=> ElectroniqueCaméras(仅修改了É,Caméras中的é保持不变)
I need to remove diacritics from uppercase characters in a string.Example : Électronique Caméras => Electronique Caméras (only the É is modified, é in Caméras remains as it is)
我正在使用以下方法,该方法仅从大写字母中删除变音符号,但是重建的字符串看起来像这样-Electronique Came?ras(é丢失了).如何正确重建字符串?
I am using the following method, which removes diacritics only from the uppercase letters, but the reconstructed string looks like this - Electronique Came?ras (é is lost). How can I reconstruct the string properly?
public static String removeDiacriticsFromUppercaseLetters(String input)
{
if (input == null)
return input;
String normalized= Normalizer.normalize(input, Normalizer.Form.NFD);
StringBuilder newString = new StringBuilder();
newString.append(normalized.charAt(0));
for (int i=1;i<normalized.length();++i)
{
//Check if this diacritic is for an uppercase letter, if yes, skip
if (Character.isUpperCase(normalized= .charAt(i-1)) && Character.getType(normalized.charAt(i)) == Character.NON_SPACING_MARK){
continue;
}
else{
newString.append(normalized.charAt(i));
}
}
return newString.toString();
}
谢谢
推荐答案
尝试以下方法:
public static String removeDiacriticsFromUppercaseLetters(String input)
{
if (input == null)
return null;
String normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
StringBuilder newString = new StringBuilder();
boolean checkDiacritics = false;
for (int i = 0; i < normalized.length(); ++i)
{
char ch = normalized.charAt(i);
if (checkDiacritics)
{
if (Character.getType(ch) == Character.NON_SPACING_MARK)
continue;
checkDiacritics = false;
}
if (Character.isUpperCase(ch))
checkDiacritics = true;
newString.append(ch);
}
//return newString.toString();
return Normalizer.normalize(newString.toString(), Normalizer.Form.NFC);
}
这篇关于如何仅删除字符串中大写字母的变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!