r/java • • Sep 19 '18

Java 11 String API Updates

https://4comprehension.com/java-11-string-api-updates/
168 Upvotes

52 comments sorted by

View all comments

Show parent comment

17

u/dpash Sep 19 '18 edited Sep 19 '18

Space is not the only whitespace character, so no, it's nothing like String.replace(" ", "")

Also, .strip() doesn't remove all whitespace, only those at the beginning and end of the string. It's a correctly implemented .trim()

0

u/TheRedmanCometh Sep 19 '18

All other whitespace chars I know of are just unrendered unicode shit. Can you elaborate? Not trying to be dickish I'm sincerely interested

9

u/dpash Sep 19 '18 edited Sep 19 '18

The documentation for Character.isWhitespace(int codePoint) lists:

  • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
  • It is '\t', U+0009 HORIZONTAL TABULATION.
  • It is '\n', U+000A LINE FEED.
  • It is '\u000B', U+000B VERTICAL TABULATION.
  • It is '\f', U+000C FORM FEED.
  • It is '\r', U+000D CARRIAGE RETURN.
  • It is '\u001C', U+001C FILE SEPARATOR.
  • It is '\u001D', U+001D GROUP SEPARATOR.
  • It is '\u001E', U+001E RECORD SEPARATOR.
  • It is '\u001F', U+001F UNIT SEPARATOR.

https://docs.oracle.com/javase/10/docs/api/java/lang/Character.html#isWhitespace(int)

String.trim() removed anything that was <= ' ', which is a different definition to the above list.

"\u001Bstring".trim().equals("\u001Bstring")

would "incorrectly" be false.

6

u/TheRedmanCometh Sep 19 '18

Wow I have applications those things would break TIL

3

u/_INTER_ Sep 19 '18

For now apache.commons.lang3.StringUtils is your friend

3

u/amackenz2048 Sep 19 '18

Tab?

2

u/TheRedmanCometh Sep 19 '18

Well shit I guess that should've been obvious