• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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?
0
chenwei
Asked:
chenwei
  • 5
1 Solution
 
zzynxSoftware engineerCommented:
I think
         split("([ ]){+}");

I'll check it for you
0
 
zzynxSoftware engineerCommented:
This is it:

        String tmp = "Hello  world   over    there";
        String parts[] = tmp.split("[ ]+");
0
 
zzynxSoftware engineerCommented:
Explanation: the delimiter is one or more times a space
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now