![](/img/trans.png)
[英]How to remove \u200B (Zero Length Whitespace Unicode Character) from String in Java?
[英]Remove character '\u202A' 8234 from string
public static String editNoHP (String noHP){
String result;
try {
if(noHP.charAt(0) == '0')
result = "62"+noHP.substring(1);
else if(noHP.charAt(0) == '+' )
result = noHP.substring(1);
else if(noHP.charAt(0) == '6')
result = noHP;
else if(noHP.charAt(0) == '6' && noHP.charAt(1) == '2')
result = noHP;
else if(noHP.charAt(0) == '9')
result = noHP;
else
result = "62"+noHP;
}
catch (Exception e){
return "";
}
return result.replaceAll("[\\s\\-\\.\\^:,]","");
}
最后,我發現我可以使用正則表達式來替換非字母數字字符。
所以這是我的壓軸功能:
public static String editNoHP (String noHPinput){
String result;
try {
noHPinput = noHPinput.trim();
String noHP = noHPinput;
noHP = noHP.replaceAll("[\\s\\-\\.\\^:,]","");
noHP = noHP.replaceAll("[^A-Za-z0-9]","");
char isinya = noHP.charAt(0);
if(isinya == '0')
result = "62"+noHP.substring(1);
else if(isinya == '+' )
result = noHP.substring(1);
else
result = noHP;
}
catch (Exception e){
return "";
}
return result;
}
此正則表達式刪除字母數字字符旁邊的所有 unicode 字符。
我遇到了同樣的問題!! 花了我幾個小時調試,因為當我打印出字符串時,第一個字符顯示為'?',所以我認為它是一個問號。 但事實並非如此!
然后我打印出第一個字符的數值,就是8234! 我當時想,wtf。 完全不知道為什么它顯示為問號。
const getFirstChar = (string) => {return string.trim().charCodeAt(0) === 8234 ? string.trim().charAt(1) : string.trim().charAt(0);};
public static String editNoHP (String noHP){
noHP = noHP.trim();
// the rest of your code...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.