java - Wrong splitting string (split(" ")) -
i have text, example:
string text = "i have text". string[] s = text.split(" ");
and have such result:
s[0] - have, s[1] - some, s[2] - good, s[3] - text
why splitter don't split text, when between space (" ") 2 or 1 symbols ("i have" , "a good"), , how solve problem?
in java, split method takes regex parameter. such, i'd recommend splitting white space:
text.split("\\s");
that way, if use different non-printable whitespace in text, split.
see http://docs.oracle.com/javase/7/docs/api/java/util/regex/pattern.html
Comments
Post a Comment