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

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -