r/coding • u/javinpaul • Sep 19 '18
Java 11 String API Updates
https://4comprehension.com/java-11-string-api-updates/5
u/PopeCumstainIIX Sep 19 '18
Wow, actual language improvements contributing to workflow and conciseness, take note here C++...
1
u/sheepdog69 Sep 19 '18
How is strip different than trim? They look the same to me. Am I missing something?
3
u/NowImAllSet Sep 19 '18
Yeah, it's in the article itself.
However, you might be asking yourself how does this one differ from String#trim? It turns out that String#strip is a modern Unicode-aware alternative that relies on the same definition of whitespace as String#isBlank.
To save you the time of digging through docs, the trim method trims whitspace as defined by the unicode space character (\\u0020) whereas the strip will use the isBlank implentation which covers a whole lot more whitspace, defined as:
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.
1
1
Sep 19 '18
Wow I’m surprised that these are new. Pythons string api already has many of these features. Now cs java classes are running out of challenges haha!
1
0
Sep 19 '18
[deleted]
2
u/TehFrozenYogurt Sep 19 '18
What do you mean? You can check if it's null and empty already by doing "== null" and "isEmpty()"
0
Sep 19 '18
[deleted]
0
u/TehFrozenYogurt Sep 19 '18
Well yeah I understand that but checking if an Object is null in Java is literally by doing == null. What's the reason for a library to write:
boolean isNull(Object o) { return o == null; }??
Also, again, Strings have isEmpty() already. If we're talking about the CharSequence interface, we already have length().
All I'm saying is that the tasks you provided are trivial to implement or is already implemented. In the OP, they're all examples of non trivial tasks.
5
u/adrianmonk Sep 19 '18 edited Sep 19 '18
They're saying they want a single method that does both checks.
Thus this:
if (s != null && !s.isEmpty()) { // do something with s }can be replaced with a less error-prone (and slightly easier to type) alternative:
if (!String.isNullOrEmpty(s)) { // do something with s }And if you use it several times in one .java file, you'd be able to static import the method to make that shorter:
import static java.lang.String.isNullOrEmpty; // ... if (!isNullOrEmpty(s1)) { // do something with s1 } if (!isNullOrEmpty(s2)) { // do something with s2 }3
u/NowImAllSet Sep 19 '18
Thanks, this is indeed what I was trying to say. Also want to add the point that it's easier to read, in my opinion. Java is notorious for lack of null safety. Adding a simple method like this would be one (very) small step towards alleviating that.
3
u/Aierou Sep 19 '18
Adding useful quality of life features to a common API? gasp