Quick example:
public class Test {
public static void main(String[] args) {
String str = \" a b\";
String[] arr = str.split(\"\\\\s+\")
The other way to trim it is to use look ahead and look behind to be sure that the whitespace is sandwiched between two non-white-space characters,... something like:
String[] arr = str.split("(?<=\\S)\\s+(?=\\S)");
The problem with this is that it doesn't trim the leading spaces, giving this result:
a
b
but nor should it as String#split(...)
is for splitting, not trimming.