I am trying to get character at 0 index in a string:<\/i>
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\\-\\.\\^:,]","");
}
Finally, I found out that I can use regex to replace non-Alphanumeric characters.
So this is my finale function :
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;
}
This regex remove all unicode characters beside Alphanumeric characters.
I came across the same issue!! Took me several hours to debug, because when I print out the string, the first character was shown as '?', so I thought it is a question mark. But it isn't!
Then I printed out the numerical value of the first character, and it is 8234! I was like, wtf. Completely have no idea why it shows as a question mark.
const getFirstChar = (string) => {return string.trim().charCodeAt(0) === 8234 ? string.trim().charAt(1) : string.trim().charAt(0);};
according to http:\/\/unicode.org\/cldr\/utility\/character.jsp?a=202A&B1=Show<\/a> \<\/code> is kind of a whitespace.<\/i>
public static String editNoHP (String noHP){
noHP = noHP.trim();
// the rest of your code...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.