我从另一个StackOverflow 邮报复制了这个代码。 但是, 我对它有些问题。 符合指定模式的物品应该替换, 但是没有替换 。
代码是:
protected String FixHexValuesInString(String str){
Log.v(TAG, "before fix: "+ str);
Matcher matcher = Pattern.compile("\\x([0-9a-f]{2})").matcher(str);
while (matcher.find()) {
int codepoint = Integer.valueOf(matcher.group(1), 16);
Log.v(TAG, "matcher group 0: " + matcher.group(0));
Log.v(TAG, "matcher group 1: " + matcher.group(1));
str = str.replaceAll(matcher.group(0), String.valueOf((char) codepoint));
}
Log.v(TAG, " after fix: "+ str);
return str;
}
我给LogCat写了个例子:
before fix: id : 1268, name : Reserva de Usos Mxfaltiples de la Cuenca del Lago de Atitlxe1n-RUMCLA (Atitlxe1n Watershed Multiple Use Reserve)
matcher group 0: xfa
matcher group 1: fa
matcher group 0: xe1
matcher group 1: e1
matcher group 0: xe1
matcher group 1: e1
after fix: id : 1268, name : Reserva de Usos Mxfaltiples de la Cuenca del Lago de Atitlxe1n-RUMCLA (Atitlxe1n Watershed Multiple Use Reserve)
有人知道为什么这行不通吗?