PDA

View Full Version : Java split string by character




andrewheard
Feb 25, 2007, 07:55 PM
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



andrewheard
Feb 25, 2007, 07:56 PM
Just as a side note, I can't have a char array. It needs to be a string array.

lazydog
Feb 25, 2007, 08:53 PM
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

ehwizard
Mar 4, 2007, 12:13 PM
foo.split( "(\\b|\\B)" )

andrewheard
Mar 4, 2007, 05:06 PM
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