android - java.lang.ArrayIndexOutOfBoundsException 在包含特殊字符的特定字符串上使用 replaceAll 时
android - java.lang.ArrayIndexOutOfBoundsException when using replaceAll on a specific string containing special characters
问题
我正在使用 Abatis 作为 ORM。当我尝试插入包含特定字符串的 json 时,它崩溃了。
我已经从 Abatis 中提取了产生错误的代码:
代码
Map<String, Object> bindParams = new HashMap<String, Object>();
bindParams.put("id", "194fa0f2-9706-493f-97ab-4eb300a8e4ed");
bindParams.put("field", "{\"Messages\":\"ERRORE durante l'invocazione del servizio. 01 - Executor [java.util.concurrent.ThreadPoolExecutor@18a96588] did not accept task: org.springframework.aop.interceptor.AsyncExecutionInterceptor@14a7c67b\",\"Errors\":1}");
String sql = "UPDATE <TABLE> SET NoteAgente = #field# WHERE Id = #id#";
if (bindParams != null) {
Iterator<String> mapIterator = bindParams.keySet().iterator();
while (mapIterator.hasNext()) {
String key = mapIterator.next();
Object value = bindParams.get(key);
if(value instanceof String && value != null)
value = value.toString().replace("'", "''");
sql = sql.replaceAll("#" + key + "#", value == null ? "null"
: "'" + value.toString() + "'");
}
}
问题出在 replaceAll 方法中,字符串 $1@14a7c67b。你也可以调试它写
String s = "onetwothree";
s = s.replaceAll("one", "@14a7c67b");
它也会崩溃。
replaceAll
采用正则表达式参数,</code> 是告诉 java 正则表达式引擎使用第一组作为替换的特殊方式。</p>
<p>您需要使用 <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence)" rel="noreferrer"><code>replace
其中 matches/replaces 字面意思的字符串:
String s = "onetwothree";
s = s.replace("one", "@14a7c67b");
如果您仍然需要使用 replaceAll
:
,您也可以转义 $
字符
s = s.replaceAll("one", "\@14a7c67b");
问题
我正在使用 Abatis 作为 ORM。当我尝试插入包含特定字符串的 json 时,它崩溃了。
我已经从 Abatis 中提取了产生错误的代码:
代码
Map<String, Object> bindParams = new HashMap<String, Object>();
bindParams.put("id", "194fa0f2-9706-493f-97ab-4eb300a8e4ed");
bindParams.put("field", "{\"Messages\":\"ERRORE durante l'invocazione del servizio. 01 - Executor [java.util.concurrent.ThreadPoolExecutor@18a96588] did not accept task: org.springframework.aop.interceptor.AsyncExecutionInterceptor@14a7c67b\",\"Errors\":1}");
String sql = "UPDATE <TABLE> SET NoteAgente = #field# WHERE Id = #id#";
if (bindParams != null) {
Iterator<String> mapIterator = bindParams.keySet().iterator();
while (mapIterator.hasNext()) {
String key = mapIterator.next();
Object value = bindParams.get(key);
if(value instanceof String && value != null)
value = value.toString().replace("'", "''");
sql = sql.replaceAll("#" + key + "#", value == null ? "null"
: "'" + value.toString() + "'");
}
}
问题出在 replaceAll 方法中,字符串 $1@14a7c67b。你也可以调试它写
String s = "onetwothree";
s = s.replaceAll("one", "@14a7c67b");
它也会崩溃。
replaceAll
采用正则表达式参数,</code> 是告诉 java 正则表达式引擎使用第一组作为替换的特殊方式。</p>
<p>您需要使用 <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence)" rel="noreferrer"><code>replace
其中 matches/replaces 字面意思的字符串:
String s = "onetwothree";
s = s.replace("one", "@14a7c67b");
如果您仍然需要使用 replaceAll
:
$
字符
s = s.replaceAll("one", "\@14a7c67b");