• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Split a string

 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yea the first word and subsequent message after it will change in size, so the fix has to be scalable
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't even know that it existed...
Nor did I until now . . .
[ March 09, 2007: Message edited by: Campbell Ritchie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic