Solved

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

Posted on 2004-10-20
6
215 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordmultiple challenge 12 116
base64 decode encode 12 119
what is the main difference between git clone and fetch ann pull and push commands 2 67
session migration servlets 2 23
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now