获取Unicode编码
package com.xs.test;
public class Test {
public static void main(String[] args) throws Exception {
int decimal = ((int)'中');
System.out.println(decimal); // Unicode十进制编码
String hex = Integer.toHexString(decimal);
System.out.println(hex); // Unicode十六进制编码
System.out.println("中".contains("\u4e2d"));
}
}输出结果:
20013
4e2d
true
过滤特殊字符ZWNJ(zero-width non-joiner)
字符ZWNJ是个不可见的特殊字符,在数据库中往往会被保存为乱码,所以要过滤掉。
文件a.xml包含两个特殊字符ZWNJ:
中华人民共和国过滤掉字符ZWNJ:
package com.xs.test;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Test2 {
public static void main(String[] args) throws IOException {
String path = Test2.class.getResource("a.xml").getFile();
String content = FileUtils.readFileToString(new File(path), "UTF-8");
System.out.println(content);
char c1 = content.charAt(content.indexOf('华') + 1);
int unicode1 = (int)c1;
String hexUnicode1 = Integer.toHexString(unicode1);
System.out.println(hexUnicode1);
char c2 = content.charAt(content.indexOf('民') + 1);
int unicode2 = (int)c2;
String hexUnicode2 = Integer.toHexString(unicode2);
System.out.println(hexUnicode2);
System.out.println("-------------过滤后------------");
System.out.println(content.replaceAll("\u200c", ""));
}
}输出结果:
中华?人民?共和国
200c
200c
-------------过滤后------------
中华人民共和国