Browse Source

Improve performance of StringUtils#trimWhitespace

Issue: SPR-16766

(cherry picked from commit 6545cab)
pull/1800/head
Oleksandr Hasheniuk 8 years ago committed by Juergen Hoeller
parent
commit
5a1d7f9c4b
  1. 16
      spring-core/src/main/java/org/springframework/util/StringUtils.java

16
spring-core/src/main/java/org/springframework/util/StringUtils.java

@ -209,14 +209,18 @@ public abstract class StringUtils {
return str; return str;
} }
StringBuilder sb = new StringBuilder(str); int beginIndex = 0;
while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) { int endIndex = str.length() - 1;
sb.deleteCharAt(0);
while (beginIndex <= endIndex && Character.isWhitespace(str.charAt(beginIndex))) {
beginIndex++;
} }
while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
sb.deleteCharAt(sb.length() - 1); while (endIndex > beginIndex && Character.isWhitespace(str.charAt(endIndex))) {
endIndex--;
} }
return sb.toString();
return str.substring(beginIndex, endIndex + 1);
} }
/** /**

Loading…
Cancel
Save