Solved

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

Posted on 2004-10-20
6
214 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
Comment Utility
I think
         split("([ ]){+}");

I'll check it for you
0
 
LVL 37

Accepted Solution

by:
zzynx earned 30 total points
Comment Utility
This is it:

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

Expert Comment

by:zzynx
Comment Utility
Explanation: the delimiter is one or more times a space
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 13

Expert Comment

by:petmagdy
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
json example 39 110
Understanding websocket example in spring 1 41
groovy example issue 10 65
Java - Why doesn't this JFrame work 3 14
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

771 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

10 Experts available now in Live!

Get 1:1 Help Now