• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Split a string

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic