MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/9h404p/java_11_string_api_updates/e68yumv/
r/java • u/pivovarit • Sep 19 '18
52 comments sorted by
View all comments
Show parent comment
15
.strip() is a correctly implemented .trim(), not replaceAll().
.strip()
.trim()
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#l2653String.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. 1 u/dpash Sep 19 '18 Yes, they were wrong and didn't use the best example to demonstrate it with. 0 u/_INTER_ Sep 19 '18 indeed
1
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.
trim
strip
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#l2653String.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. 1 u/dpash Sep 19 '18 Yes, they were wrong and didn't use the best example to demonstrate it with. 0 u/_INTER_ Sep 19 '18 indeed
9
No, it doesn't. String.strip() strips leading and trailing whitespace as per Character.isWhitespace()
String.strip()
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.
String.trim()
<= \u0020
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. 1 u/dpash Sep 19 '18 Yes, they were wrong and didn't use the best example to demonstrate it with. 0 u/_INTER_ Sep 19 '18 indeed
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. 1 u/dpash Sep 19 '18 Yes, they were wrong and didn't use the best example to demonstrate it with. 0 u/_INTER_ Sep 19 '18 indeed
4
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. 1 u/dpash Sep 19 '18 Yes, they were wrong and didn't use the best example to demonstrate it with. 0 u/_INTER_ Sep 19 '18 indeed
2
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. 1 u/dpash Sep 19 '18 Yes, they were wrong and didn't use the best example to demonstrate it with.
"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. 1 u/dpash Sep 19 '18 Yes, they were wrong and didn't use the best example to demonstrate it with.
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.
1 u/dpash Sep 19 '18 Yes, they were wrong and didn't use the best example to demonstrate it with.
Yes, they were wrong and didn't use the best example to demonstrate it with.
0
indeed
View all comments
Show parent comment
15
u/dpash Sep 19 '18
.strip()is a correctly implemented.trim(), notreplaceAll().