Solved

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

Posted on 2004-10-20
6
216 Views
Last Modified: 2010-08-05
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
Comment
Question by:chenwei
  • 5
6 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12357200
I think
         split("([ ]){+}");

I'll check it for you
0
 
LVL 37

Accepted Solution

by:
zzynx earned 30 total points
ID: 12357227
This is it:

        String tmp = "Hello  world   over    there";
        String parts[] = tmp.split("[ ]+");
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12357238
Explanation: the delimiter is one or more times a space
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 13

Expert Comment

by:petmagdy
ID: 12357288
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12357324
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12357387
Thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 108
mockito example issue 8 73
table example 4 29
spring jars download 1 28
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question