From 68cf95f499f644eff2a535ce86a1a20d85f2204e Mon Sep 17 00:00:00 2001 From: liuzhifei <2679431923@qq.com> Date: Fri, 19 Nov 2021 17:33:52 +0800 Subject: [PATCH] Use String::strip in StringUtils::trimWhitespace See gh-27703 --- .../org/springframework/util/StringUtils.java | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 313979e87e0..9f45c8bc2be 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -224,18 +224,7 @@ public abstract class StringUtils { return str; } - int beginIndex = 0; - int endIndex = str.length() - 1; - - while (beginIndex <= endIndex && Character.isWhitespace(str.charAt(beginIndex))) { - beginIndex++; - } - - while (endIndex > beginIndex && Character.isWhitespace(str.charAt(endIndex))) { - endIndex--; - } - - return str.substring(beginIndex, endIndex + 1); + return str.strip(); } /** @@ -272,11 +261,7 @@ public abstract class StringUtils { return str; } - int beginIdx = 0; - while (beginIdx < str.length() && Character.isWhitespace(str.charAt(beginIdx))) { - beginIdx++; - } - return str.substring(beginIdx); + return str.stripLeading(); } /** @@ -290,11 +275,7 @@ public abstract class StringUtils { return str; } - int endIdx = str.length() - 1; - while (endIdx >= 0 && Character.isWhitespace(str.charAt(endIdx))) { - endIdx--; - } - return str.substring(0, endIdx + 1); + return str.stripTrailing(); } /**