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.

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 :)