[ https://issues.apache.org/jira/browse/LANG-1453?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16923376#comment-16923376 ]
Chen edited comment on LANG-1453 at 9/10/19 6:22 AM: ----------------------------------------------------- Add replace method in RegExUtils.java {code:java} public static String replace(final String text, final String regex, final String replacement, int max, final boolean ignoreCase) { if (text == null || regex == null || replacement == null || max == 0) { return text; } Pattern pattern = ignoreCase ? Pattern.compile(regex, Pattern.CASE_INSENSITIVE) : Pattern.compile(regex); Matcher matcher = pattern.matcher(text); boolean result = matcher.find(); if (!result) { return text; } StringBuffer sb = new StringBuffer(); do { matcher.appendReplacement(sb, replacement); if (--max == 0) { break; } result = matcher.find(); } while (result); matcher.appendTail(sb); return sb.toString(); } {code} Modify the replace method in StringUtils.java {code:java} private static String replace(final String text, String searchString, final String replacement, int max, final boolean ignoreCase) { if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) { return text; } return RegExUtils.replace(text, searchString, replacement, max, ignoreCase); } {code} Let's test it together. was (Author: guoping1): Add replace method in RegExUtils.java public static String replace(final String text, final String regex, final String replacement, int max, final boolean ignoreCase) { if (text == null || regex == null || replacement == null || max == 0) { return text; } Pattern pattern = ignoreCase ? Pattern.compile(regex, Pattern.CASE_INSENSITIVE) : Pattern.compile(regex); Matcher matcher = pattern.matcher(text); boolean result = matcher.find(); if (!result) { return text; } StringBuffer sb = new StringBuffer(); do { matcher.appendReplacement(sb, replacement); if (--max == 0) { break; } result = matcher.find(); } while (result); matcher.appendTail(sb); return sb.toString(); } Modify the replace method in StringUtils.java private static String replace(final String text, String searchString, final String replacement, int max, final boolean ignoreCase) { if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) { return text; } return RegExUtils.replace(text, searchString, replacement, max, ignoreCase); } Let's test it together. > StringUtils.removeIgnoreCase("İa", "a") throws IndexOutOfBoundsException > ------------------------------------------------------------------------ > > Key: LANG-1453 > URL: https://issues.apache.org/jira/browse/LANG-1453 > Project: Commons Lang > Issue Type: Bug > Components: lang.text.* > Affects Versions: 3.8.1 > Reporter: Thomas Neerup > Priority: Critical > Time Spent: 2h 40m > Remaining Estimate: 0h > > *try* { > String s = StringUtils._removeIgnoreCase_("İa", "a"); > } *catch* (Exception e) { > e.printStackTrace(); > } > output > java.lang.IndexOutOfBoundsException: start 3, end 2, s.length() 2 > at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:539) > -- This message was sent by Atlassian Jira (v8.3.2#803003)