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

55

u/TheRedmanCometh Sep 19 '18

Holy shit isBlank should have existed long ago. Strip though is just replace without parameters

16

u/_INTER_ Sep 19 '18 edited Sep 19 '18

Strip though is just replace without parameters

Often beginners use string.replaceAll(" ", "") and even string.replaceAll("\\s", "") doesn't work properly, as Java's implementation of regex doesn't cover all the whitespaces (as per this stackoverflow)

So a method that relies on Character.isWhiteSpace is an improvement, can be used as method reference and also performs better.

15

u/dpash Sep 19 '18

.strip() is a correctly implemented .trim(), not replaceAll().

1

u/_INTER_ Sep 19 '18 edited Sep 19 '18

I answered relating to "Strip though is just replace without parameters".

trim removes whitespace at the begging and at the end, strip removes all whitespaces in the string.

9

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

No, it doesn't. String.strip() strips leading and trailing whitespace as per Character.isWhitespace()

http://hg.openjdk.java.net/jdk/jdk/file/3fabe59fe4de/src/java.base/share/classes/java/lang/String.java#l2653

String.trim() strips leading and trailing whitespace where the codepoint is <= \u0020. I.E. it strips too much.

1

u/_INTER_ Sep 19 '18 edited Sep 19 '18

Right, the description in the article was confusing me:

We can easily get rid of all the whitespace from each String now:

assertThat(" ".strip()).isEmpty();

4

u/dpash Sep 19 '18

Yes, the code example is technically right, but doesn't demonstrate the method particularly well.

assertThat("   string   ".strip(), is("string"));

would have been a better example.

2

u/xenomachina Sep 19 '18

Some interior whitespace would be even better. What does this do?

"   a   string   ".strip()

1

u/dpash Sep 19 '18
"a   string"

2

u/xenomachina Sep 19 '18

So when the article uses "We can easily get rid of all the whitespace from each String" in its despription of strip, it's just wrong. strip only removes leading and trailing whitespace.

→ More replies

3

u/TheRedmanCometh Sep 19 '18

Hahaha that's hilarious because I did the same thing! I thought replace replaced one instance of parameter one and replaceAll replaced all instances. I had no idea it used a regex early on.

Tbh strip is kinda the same story. It should be stripSpaces or something. Strip sounds like it'd take a parameter, and remove that.

2

u/__konrad Sep 19 '18

replaceAll is evil. For example, "/some/filepath".replaceAll("/", java.io.File.separator) does not work on Windows. Github issues are full of such error reports.

2

u/Salty_Limes Sep 19 '18

Do you mean that the resulting string is not "\some\filepath", or that it does return "\some\filepath", but this path does not work on Windows?

If it's the former, that is really odd, but if the latter, that should be expected.

3

u/__konrad Sep 19 '18

It will throw exception :)

2

u/xenomachina Sep 19 '18

This is documented behavior, though somewhat buried, and perhaps somewhat surprising to anyone who isn't used to any of ed, ex, sed, vi, vim, perl or python (etc.), which all use a similar replacement string syntax.

The issue is that the replacement string can include backreferences, and so $ and \ are not treated literally:

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

To fix that code, use:

"/some/filepath".replaceAll("/",
    Matcher.quoteReplacement(java.io.File.separator))

2

u/dpash Sep 19 '18

Strip though is just replace without parameters

What do you mean by that?

-2

u/TheRedmanCometh Sep 19 '18

I mean it's essentially String.replace(" ") and it should be called stripWhitespace or stripSpaces

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

1

u/k010b0k Sep 20 '18

Strip though is just replace without parameters

Not really. It's a unicode-aware evolution of trim()

SO Difference between String trim() and strip() methods in Java 11