Java split string by character

Discussion in 'Mac Programming' started by andrewheard, Feb 25, 2007.

  1. macrumors regular

    Joined:
    May 16, 2005
    Location:
    Ontario, Canada
    #1
    Hi,
    I need to be able to split a string into individual characters. Currently I have accomplished this using SINstring = target.split(" ");
    and I have to enter the SIN as "1 2 3 4 5 6 7 8 9" but how can I have it so that I can input "123456789". Splitting it by any number with \d causes it to output nothing into the array because the delimiter gets removed and since all of them are numbers they go bye bye.

    Thanks for any input,

    Andrew
     
  2. thread starter macrumors regular

    Joined:
    May 16, 2005
    Location:
    Ontario, Canada
    #2
    Just as a side note, I can't have a char array. It needs to be a string array.
     
  3. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    How about this:-

    String[] s = test.replaceAll( "(\\w)", "*$1" ).split( "\\s*\\*\\s*" ) ;

    This will, I think, handle strings with arbitrary spaces like "12 3 45 678 9"

    b e n
     
  4. macrumors newbie

    Joined:
    Jan 28, 2003
    Location:
    New York, NY
    #4
    foo.split( "(\\b|\\B)" )
     
  5. thread starter macrumors regular

    Joined:
    May 16, 2005
    Location:
    Ontario, Canada
    #5
    Thanks for the replies and sorry for the delayed response. I ended up writing the thing so i didnt't even have to split the String. Don't know how to explain it but it works.

    Thanks again,

    Andrew
     

Share This Page