Solved

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

Posted on 2004-10-20
6
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 49
SequenceInputStream example 3 38
Fast way to search item into Java Array (Rhino compatible) 2 47
Overriding a method 3 12
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 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