Java equivalent for .charCodeAt()
在JavaScript中,
1 2 3 4 | public int charCodeAt(char c) { int x; return x = (int) c; } |
如果我在Java中有一个字符串,那么如何获得字符串中一个字符的Unicode值,就像
Java具有相同的方法:Character.codePointAt(CharSequence seq,int index);
有一种方法可以过滤所需的特殊字符。 只需检查ASCII表
希望能帮助到你
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class main { public static void main(String args[]) { String str = args[0]; String bstr =""; String[] codePointAt = new String[str.length()]; if (str !="") { for (int j = 0; j < str.length(); j++) { int charactercode=Character.codePointAt(str, j); //CHECK on ASCII TABLE THE SPECIAL CHARS YOU NEED if( (charactercode>31 && charactercode<48) || (charactercode>57 && charactercode<65) || (charactercode>90 && charactercode<97) || (charactercode>127) ) { codePointAt[ j] ="&"+String.valueOf(charactercode)+";"; } else { codePointAt[ j] = String.valueOf( str.charAt(j) ); } } for (int j = 0; j < codePointAt.length; j++) { System.out.println("CODE"+j+" ->"+ codePointAt[j]); } } } } |
输出值
1 2 3 4 5 6 7 8 9 10 11 12 | call with ("TRY./&asda") CODE 0 ->T CODE 1 ->R CODE 2 ->Y CODE 3 ->&46; CODE 4 ->&47; CODE 5 ->&38; CODE 6 ->a CODE 7 ->s CODE 8 ->d CODE 9 ->a |
试试这个:
1 2 3 |
1 | short unicode = string.charAt(index); |