So I have a string:
String myString = "[tab][space]this[space][tab]is[space]a[space][tab]string";
String[] mySplit = myString.split("\\s+");
System.out.println(mySplit.length);
for (String s : mySplit)
System.out.println(s);
Gives me:
5
[whitespace]
this
is
a
string
Why is this and how do I stop that first position from being filled with whitespace?
The way
split()is defined, if the input string starts with the delimiter then the first returned segment will be empty. This might be a little counter-intuitive, but considerYou would want to get back four elements.
If you need to remove leading whitespace then use
trim()first.