Solved

Easy question about breaking apart strings

Posted on 2005-05-03
13
216 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
ID: 13917366
use split()
0
 
LVL 86

Expert Comment

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

Accepted Solution

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

Expert Comment

by:zzynx
ID: 13917426
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
ID: 13917436
Remark: split() is available since 1.4
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13917453
No you won't have that with split(" ") ;-)
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 9

Expert Comment

by:cyberdevil67
ID: 13917460
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
ID: 13917469
If you're unfortunate enough to be stuck with Java < 1.4 then use java.util.StringTokenizer
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13917480
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
ID: 13917502
On my machine:

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

prints

3
0
 
LVL 86

Expert Comment

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

Expert Comment

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

Expert Comment

by:zzynx
ID: 13917667
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

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
best (free) software to access postgres db (java) 1 33
base64 decode encode 12 122
Java DateChooser? 3 29
xampp tool 12 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 covers a step-by-step guide to install VisualVM launcher in eclipse.

867 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

20 Experts available now in Live!

Get 1:1 Help Now