Link to home
Start Free TrialLog in
Avatar of chenwei
chenwei

asked on

How to use String.split() to split this string?

My string looks as follow:

str = "fist second  third     forth         fifth";

Can I use split() to split it into 5 sub strings, i.e. without containning the spaces?
Avatar of zzynx
zzynx
Flag of Belgium image

I think
         split("([ ]){+}");

I'll check it for you
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Explanation: the delimiter is one or more times a space
Or simply just use java.util.StringTokenizer like this:

StringTokenizer st = new StringTokenizer("fist second  third     forth         fifth");
     while (st.hasMoreTokens()) {
         System.out.println(st.nextToken());
     }
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Thanks