Summary
This feature introduces three String instance methods for removal of Unicode white space from the beginning and end of a string.
Problem
String::trim has existed from early days of Java when Unicode had not fully evolved to the standard we widely use today.
The definition of space used by String::trim is any code point less than or equal to the space code point (\u0020), commonly referred to as ASCII or ISO control characters.
Unicode-aware trimming routines should use Character::isWhitespace(int).
Additionally, developers have not been able to specifically remove indentation white space or to specifically remove trailing white space.
Solution
Introduce trimming methods that are Unicode white space aware and provide additional control of leading only or trailing only.
Specification
/**
* Returns a string whose value is this string, with all leading
* and trailing {@link Character#isWhitespace(int) white space}
* removed.
* <p>
* If this {@code String} object represents an empty string,
* or if all code points in this string are
* {@link Character#isWhitespace(int) white space}, then an empty string
* is returned.
* <p>
* Otherwise, returns a substring of this string beginning with the first
* code point that is not a {@link Character#isWhitespace(int) white space}
* up to and including the last code point that is not a
* {@link Character#isWhitespace(int) white space}.
* <p>
* This method may be used to strip
* {@link Character#isWhitespace(int) white space} from
* the beginning and end of a string.
*
* @return a string whose value is this string, with all leading
* and trailing white space removed
*
* @see Character#isWhitespace(int)
*
* @since 11
*/
public String strip() {
/**
* Returns a string whose value is this string, with all leading
* {@link Character#isWhitespace(int) white space} removed.
* <p>
* If this {@code String} object represents an empty string,
* or if all code points in this string are
* {@link Character#isWhitespace(int) white space}, then an empty string
* is returned.
* <p>
* Otherwise, returns a substring of this string beginning with the first
* code point that is not a {@link Character#isWhitespace(int) white space}
* up to to and including the last code point of this string.
* <p>
* This method may be used to trim
* {@link Character#isWhitespace(int) white space} from
* the beginning of a string.
*
* @return a string whose value is this string, with all leading white
* space removed
*
* @see Character#isWhitespace(int)
*
* @since 11
*/
public String stripLeading() {
/**
* Returns a string whose value is this string, with all trailing
* {@link Character#isWhitespace(int) white space} removed.
* <p>
* If this {@code String} object represents an empty string,
* or if all characters in this string are
* {@link Character#isWhitespace(int) white space}, then an empty string
* is returned.
* <p>
* Otherwise, returns a substring of this string beginning with the first
* code point of this string up to and including the last code point
* that is not a {@link Character#isWhitespace(int) white space}.
* <p>
* This method may be used to trim
* {@link Character#isWhitespace(int) white space} from
* the end of a string.
*
* @return a string whose value is this string, with all trailing white
* space removed
*
* @see Character#isWhitespace(int)
*
* @since 11
*/
public String stripTrailing() {
- csr of
-
JDK-8200377 String::strip, String::stripLeading, String::stripTrailing
-
- Resolved
-