This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Split a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Split a string" Watch "Split a string" New topic
Author

Split a string

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey guys,
Could i get a hand from ye splitting a string??
Im nearly there but would like to find out the best way to finsih up.
Anyway i have a string say;

Now i would like to split the string by white spaces;
I use

which splits all the white spaces, i would like too just split the first white space.
so ideally i would like to finish with two string variables;

By splitting into an array im not sure i can do what i want, Any help please??

Mark
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
oh yea the first word and subsequent message after it will change in size, so the fix has to be scalable
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38511
    
  23
Try this, mostly copied from your own posting:-(You can leave out the = null bit.). . . thenSee what you get. See whether you can find any way of getting your output into two arrays.
Find the System class in the API and the Arrays class as well; you might find methods there for copying arrays into each other.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
So basically what you want to do is finding the very first whitespace in the string, and then get the substring before that location, as well as the substring behind that location, don't you?

If so, it shouldn't be too hard to translate into code, I'd think...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Not sure that's what's wanted, is it? Would not String.split(String, int) do the trick? The int is the maximum number of times to split.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey Campbell,
Thanks i tried that and it returned;
arrayTest[0] = testing
arrayTest[1] = This
arrayTest[2] = is
arrayTest[3] = a
arrayTest[4] = test

I dont really get the [%d] = %s%n, just escape characters is it?? Ill have to look more in to it as you suggested.

So I tried this and it seems to work,

So strResult1 will contain "testing"
And strResult2 will have "this is a test"
Which is what i want, Campbell what do you think, would this be a good solution or is there an easier way?? Thanks



Mark
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
Hi Mark,

Peter Chase mentioned the split( String regex, int limit ) method from the String class, which would do what you want with less code.

If you try something like






and print out the results, you will get an idea of what it does.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Katrina, pater,and all

i see, we are gettin there now ha ha



Right this gets the job done with far less code, thanks all for your help.

Best!
Mark
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38511
    
  23
Yes, you are getting there. And I think the two-parameter version of String.split() works better, as you have found out. But I think you need to check the String class carefully in the API; I think split(" ", 2) might be what you actually want.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38511
    
  23
. . . and for the %s %n, look up the preamble to the java.util.Formatter class. Note these formatting sequences only work in Java 5 or 6.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Peter Chase:
Would not String.split(String, int) do the trick?


It would!

Didn't even know that it existed...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38511
    
  23
Didn't even know that it existed...
Nor did I until now . . .
[ March 09, 2007: Message edited by: Campbell Ritchie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Split a string