8200377: String::strip, String::stripLeading, String::stripTrailing

Reviewed-by: sundar, rriggs
This commit is contained in:
Jim Laskey 2018-05-14 09:40:48 -03:00
parent ec2d9845e0
commit 7906014509
4 changed files with 300 additions and 1 deletions

View file

@ -2602,7 +2602,7 @@ public final class String
* Returns a string whose value is this string, with all leading
* and trailing space removed, where space is defined
* as any character whose codepoint is less than or equal to
* {@code '\u005Cu0020'} (the space character).
* {@code 'U+0020'} (the space character).
* <p>
* If this {@code String} object represents an empty character
* sequence, or the first and last characters of character sequence
@ -2636,6 +2636,98 @@ public final class String
return ret == null ? this : ret;
}
/**
* 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() {
String ret = isLatin1() ? StringLatin1.strip(value)
: StringUTF16.strip(value);
return ret == null ? this : ret;
}
/**
* 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() {
String ret = isLatin1() ? StringLatin1.stripLeading(value)
: StringUTF16.stripLeading(value);
return ret == null ? this : ret;
}
/**
* 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() {
String ret = isLatin1() ? StringLatin1.stripTrailing(value)
: StringUTF16.stripTrailing(value);
return ret == null ? this : ret;
}
/**
* This object (which is already a string!) is itself returned.
*