Browse Source

optimize StringUtils trimLeadingWhitespace() / trimTrailingWhitespace() & trimLeadingCharacter() / trimTrailingCharacter() utility methods

pull/25823/head
Shenker93 5 years ago committed by Juergen Hoeller
parent
commit
ad5072a43c
  1. 32
      spring-core/src/main/java/org/springframework/util/StringUtils.java

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

@ -271,11 +271,11 @@ public abstract class StringUtils { @@ -271,11 +271,11 @@ public abstract class StringUtils {
return str;
}
StringBuilder sb = new StringBuilder(str);
while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) {
sb.deleteCharAt(0);
int beginIdx = 0;
while (beginIdx < str.length() && Character.isWhitespace(str.charAt(beginIdx))) {
beginIdx++;
}
return sb.toString();
return str.substring(beginIdx);
}
/**
@ -289,11 +289,11 @@ public abstract class StringUtils { @@ -289,11 +289,11 @@ public abstract class StringUtils {
return str;
}
StringBuilder sb = new StringBuilder(str);
while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
sb.deleteCharAt(sb.length() - 1);
int endIdx = str.length() - 1;
while (endIdx >= 0 && Character.isWhitespace(str.charAt(endIdx))) {
endIdx--;
}
return sb.toString();
return str.substring(0, endIdx + 1);
}
/**
@ -307,11 +307,11 @@ public abstract class StringUtils { @@ -307,11 +307,11 @@ public abstract class StringUtils {
return str;
}
StringBuilder sb = new StringBuilder(str);
while (sb.length() > 0 && sb.charAt(0) == leadingCharacter) {
sb.deleteCharAt(0);
int beginIdx = 0;
while (beginIdx < str.length() && leadingCharacter == str.charAt(beginIdx)) {
beginIdx++;
}
return sb.toString();
return str.substring(beginIdx);
}
/**
@ -325,11 +325,11 @@ public abstract class StringUtils { @@ -325,11 +325,11 @@ public abstract class StringUtils {
return str;
}
StringBuilder sb = new StringBuilder(str);
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == trailingCharacter) {
sb.deleteCharAt(sb.length() - 1);
int endIdx = str.length() - 1;
while (endIdx >= 0 && trailingCharacter == str.charAt(endIdx)) {
endIdx--;
}
return sb.toString();
return str.substring(0, endIdx + 1);
}
/**

Loading…
Cancel
Save