Skip to content

Commit bf39492

Browse files
committed
Introduce StringUtils.trimAllWhitespace(CharSequence)
Closes gh-28757
1 parent 007bded commit bf39492

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,28 +241,45 @@ public static String trimWhitespace(String str) {
241241
}
242242

243243
/**
244-
* Trim <i>all</i> whitespace from the given {@code String}:
244+
* Trim <em>all</em> whitespace from the given {@code CharSequence}:
245245
* leading, trailing, and in between characters.
246-
* @param str the {@code String} to check
247-
* @return the trimmed {@code String}
246+
* @param text the {@code CharSequence} to check
247+
* @return the trimmed {@code CharSequence}
248+
* @since 5.3.22
249+
* @see #trimAllWhitespace(String)
248250
* @see java.lang.Character#isWhitespace
249251
*/
250-
public static String trimAllWhitespace(String str) {
251-
if (!hasLength(str)) {
252-
return str;
252+
public static CharSequence trimAllWhitespace(CharSequence text) {
253+
if (!hasLength(text)) {
254+
return text;
253255
}
254256

255-
int len = str.length();
256-
StringBuilder sb = new StringBuilder(str.length());
257+
int len = text.length();
258+
StringBuilder sb = new StringBuilder(text.length());
257259
for (int i = 0; i < len; i++) {
258-
char c = str.charAt(i);
260+
char c = text.charAt(i);
259261
if (!Character.isWhitespace(c)) {
260262
sb.append(c);
261263
}
262264
}
263265
return sb.toString();
264266
}
265267

268+
/**
269+
* Trim <em>all</em> whitespace from the given {@code String}:
270+
* leading, trailing, and in between characters.
271+
* @param str the {@code String} to check
272+
* @return the trimmed {@code String}
273+
* @see #trimAllWhitespace(CharSequence)
274+
* @see java.lang.Character#isWhitespace
275+
*/
276+
public static String trimAllWhitespace(String str) {
277+
if (str == null) {
278+
return null;
279+
}
280+
return trimAllWhitespace((CharSequence) str).toString();
281+
}
282+
266283
/**
267284
* Trim leading whitespace from the given {@code String}.
268285
* @param str the {@code String} to check

0 commit comments

Comments
 (0)