我有一个大字符串,我需要将所有非字母数字字符转换为unicode
例如
输入字符串:abc12/dad-das/das_sdj
输出字符串:abc12:002Fdad:002Ddas:002Fdas:002Fsdj
目前我正在使用此功能
for (char c : str.toCharArray()) {
System.out.printf(":%04X \n", (int) c);
}
有更好的方法吗?
答案 0 :(得分:3)
以下是两种方法:
// Looping over string characters
private static String convert(String input) {
StringBuilder buf = new StringBuilder(input.length() + 16);
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
buf.append(c);
else
buf.append(String.format(":%04X", (int) c));
}
return buf.toString();
}
// Using regular expression
private static String convert(String input) {
StringBuffer buf = new StringBuffer(input.length() + 16);
Matcher m = Pattern.compile("[^a-zA-Z0-9]").matcher(input);
while (m.find())
m.appendReplacement(buf, String.format(":%04X", (int) m.group().charAt(0)));
return m.appendTail(buf).toString();
}
测试
System.out.println(convert("abc12/dad-das/das_sdj"));
输出
abc12:002Fdad:002Ddas:002Fdas:005Fsdj