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?
chenweiAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
This is it:

        String tmp = "Hello  world   over    there";
        String parts[] = tmp.split("[ ]+");
0
 
zzynxSoftware engineerCommented:
I think
         split("([ ]){+}");

I'll check it for you
0
 
zzynxSoftware engineerCommented:
Explanation: the delimiter is one or more times a space
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
petmagdyCommented:
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());
     }
0
 
zzynxSoftware engineerCommented:
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.
0
 
zzynxSoftware engineerCommented:
Thanks
0
All Courses

From novice to tech pro — start learning today.