Improve StringUtils#trimAllWhitespace

Prior to this commit, StringUtils#trimAllWhitespace(String str) was
unecessary slower. Using sb.deleteCharAt(index) leads to a complete
copy of the char[]
This commit is contained in:
hengyunabc 2014-06-13 18:36:50 +08:00 committed by Stephane Nicoll
parent 98a224fed9
commit 4d328d6188
1 changed files with 6 additions and 8 deletions

View File

@ -216,14 +216,12 @@ public abstract class StringUtils {
if (!hasLength(str)) {
return str;
}
StringBuilder sb = new StringBuilder(str);
int index = 0;
while (sb.length() > index) {
if (Character.isWhitespace(sb.charAt(index))) {
sb.deleteCharAt(index);
}
else {
index++;
int len = str.length();
StringBuilder sb = new StringBuilder(str.length());
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
if (!Character.isWhitespace(c)) {
sb.append(c);
}
}
return sb.toString();