Solved

Easy question about breaking apart strings

Posted on 2005-05-03
13
215 Views
Last Modified: 2010-03-31
In PHP there is a function to explode a string to where if there is white space it will break each word out into an array.  How would I do that in java?

So say I have a string come in with "red condor fly" and I need to get "red" "condor" "fly" how would I get those three words seperated, or actually x number of words.
0
Comment
Question by:filament90
  • 6
  • 6
13 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
use split()
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
String[] tokens = s.split(" ");
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
Comment Utility
String parts[] = "red condor fly".split("\\s+");
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
If you have the string "red   condor fly"
with
>> String[] tokens = s.split(" ");
You'll have
- tokens[0] = "red"
- tokens[1] = ""
- tokens[2] = ""
- tokens[3] = "condor"
- tokens[4] = "fly"

with

>> String parts[] = s.split("\\s+");
you'll have
- parts[0] = "red"
- parts[1] = "condor"
- parts[2] = "fly"


0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Remark: split() is available since 1.4
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
No you won't have that with split(" ") ;-)
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 9

Expert Comment

by:cyberdevil67
Comment Utility
Hi filament90,

 As everyone else has said but here is more info on the subject http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Cheers!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
If you're unfortunate enough to be stuck with Java < 1.4 then use java.util.StringTokenizer
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Well, I just tested...

        String tokens[] = "red   condor fly".split(" ");
        for (int i=0; i<tokens.length; i++)
            System.out.println("<" + tokens[i] + ">");

prints out:

<red>
<>
<>
<condor>
<fly>
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
On my machine:

System.out.println("red condor fly".split(" ").length);

prints

3
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I see what you're saying. If you have unequal spaces, yes.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
(and you want that fact to be ignored)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanks for accepting, flament90

>> I see what you're saying. If you have unequal spaces, yes.
>> (and you want that fact to be ignored)
Exactly ;°)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 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

12 Experts available now in Live!

Get 1:1 Help Now