How to split a string to non empty words if it might include a separator like tab on first place

Knut Larsen :

I have a strings that I want to split on words. I use String[] words = line.split("\\s+"); Problem that some if some them start with tab separator like "\t word1 \t word2....". then as a result of split I get array with 1st element "", 2nd "word1", 3rd: "word2" ... How to modify expression split("\s+") if I don't want to get any empty "" words in split result? (Result of split should have 1st element: "word1")

Wawa2727 :

You may want to trim the word in the first place, so you no longer have spaces before the first character and after the last one, and then you could start splitting.

Example :

String[] words = line.trim().split("\\s+");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140712&siteId=1
Recommended