What would be a groovy way to conditionally wrap a string given the wrapping characters?
Having a method/closure would seem a good way to do it, but I'm struggling to write it.
/**
* Wraps a string with specified characters, uses "(" and ")" by default.
*
* e.g assert "Doctor Who".wrap() == "(Doctor Who)"
*
* @param left Character used on the left of the string, "(" by default
* @param right Character used on the right of the string, ")" by default
* @return String wrapped with the specified Characters
* @since 4.8.6
*/
public static String wrap(String self, Character left = "(", Character right = ")") {
return left + self + right
}
1.
I'm inclined not to add this, since it seems to me it's a complicated non-standard way of doing something simple and standardized, also "wrap" usually refers to newlines, so it adds confusion there as well:
It's really not an issue however, as I can define it in my format.
I did change the name to "surround", inspired by https://github.com/tpope/vim-surround
1.
In this particular instance, you can actually just use List syntax [...] since the String representation of List just so happen to do exactly what we want:
Since java.util.stream.Collectors.joining() has the same API, this should be at least somewhat intuitive.
2.
That's supposed to be a ➔ character. Strange. I'm on macOS / Chrome right now as well. The javadoc output doesn't seem to add any encoding meta tags, but it should default to UTF-8 nevertheless.