StringBuilder上的IndexOutOfBoundException

时间:2017-03-25 23:43:08

标签: java indexoutofboundsexception stringbuilder

我正在尝试使用以下代码替换pi与* pi,但是它会抛出完全意外的异常。匹配器如何查看索引高于字符串长度?

private void makeEvaluationStringExpressionMXParserCompliant() {
        Pattern multiply = Pattern.compile(mContext.getString(R.string.string_multiply));
        Pattern pi = Pattern.compile(mContext.getString(R.string.string_pie));
        Pattern e = Pattern.compile(mContext.getString(R.string.string_e));

        Log.e("wingoku", "pi: "+ pi.toString() + " completeString: "+ mEvaluationStringExpressionBuilder.toString());

       replaceAll(mEvaluationStringExpressionBuilder, pi, "*pi");
    }

    private void replaceAll(StringBuilder sb, Pattern pattern, String replacement) {
        Matcher m = pattern.matcher(sb);
        int start = 0;
        while (m.find(start)) {
            sb.replace(m.start(), m.end(), replacement);
            start = m.start() + replacement.length();
        }
    }

例外:

 java.lang.IndexOutOfBoundsException: start=3; length=2
                                                     at java.util.regex.Matcher.find(Matcher.java:339)
                                                     at com.app.calculator.utils.StringExpressionFactory.replaceAll(StringExpressionFactory.java:68)
                                                     at com.app.calculator.utils.StringExpressionFactory.makeEvaluationStringExpressionMXParserCompliant(StringExpressionFactory.java:61)
                                                     at com.app.calculator.utils.StringExpressionFactory.createExpression(StringExpressionFactory.java:31)

2 个答案:

答案 0 :(得分:2)

问题是您传递的初始索引超出了您尝试查找匹配项的字符串的长度。方法文档说明如下:

  

抛出:IndexOutOfBoundsException - 如果start小于零或者start大于输入序列的长度。

您需要添加一项检查以查看初始索引是否有效:

while (start < sb.length() && m.find(start)) {
    sb.replace(m.start(), m.end(), replacement);
    start = m.start() + replacement.length();
}

答案 1 :(得分:0)

你对开始位置有误。 你可以检查一下。以下源代码:

public static void replaceAll(StringBuilder sb, Pattern pattern, String replacement) {
Matcher m = pattern.matcher(sb);
while(m.find()) {
    sb.replace(m.start(), m.end(), replacement);
}

}

并且您最好编写代码。你不需要进行替换所有功能

 StrBuilder replaceAll(char search, char replace) 

在整个构建器中用替换字符替换搜索字符。

https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/text/StrBuilder.html#replaceAll(java.lang.String,%20java.lang.String)