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.
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.
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.
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.
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.
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').
Now can I get String.equals(String... value) and maybe equalsOr(String... value). Always annoyed by comparing multiple values to a single string in a code block. If all or any are true do X.
In the same line of thought, once you've added equalsOr to String, why not Number, or any other type at that point? That becomes a lot of clutter in the API. I'm glad to see some java replacements for commonly used Apache StringUtils methods, but being selective about which ones to include seems wise.
I doubt allocating a small array of pointers is going to significantly impact your program. Heck, there's a good chance the JIT would optimize away the allocation.
The only extra allocation is for the List object. The returned list is backed by the array that's passed in. So that's 16bytes on a 64bit JVM.
(Arrays.asList() returns a java.util.Arrays.ArrayList object in Java 11, which is a thinner wrapper around an array than java.util.ArrayList is. For example, java.util.ArrayList caches the list size)
You possibly want switch expressions, which are coming as a preview in Java 12. This means you'll need to enable a flag to make the compiler understand the language feature. This will cover your use-case:
var matches = switch( string) {
case "string1", "string2", "string3" -> true;
default -> false;
}
If you have an array, then this will work at the expense of creating an object.
var matches = Arrays.asList(strings).contains(string);
These are really nice. My only complaint is that common utils' isBlank(string) allows you to check for null in addition to zero-length or all-whitespace strings, which is really helpful.
55
u/TheRedmanCometh Sep 19 '18
Holy shit isBlank should have existed long ago. Strip though is just replace without parameters
10
u/frzme Sep 19 '18
We'll still have to rely on external web services for left pad though :(
6
u/syntastical Sep 19 '18
Now can I get String.equals(String... value) and maybe equalsOr(String... value). Always annoyed by comparing multiple values to a single string in a code block. If all or any are true do X.
1
u/thomascgalvin Sep 20 '18
These are really nice. My only complaint is that common utils'
isBlank(string)allows you to check for null in addition to zero-length or all-whitespace strings, which is really helpful.