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.

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.