PDA

View Full Version : split


BubikolRamios
03-01-2009, 06:27 PM
String[] strings = param.split("\\|");

now if param is like : "p1|p2||p4||||||";

the last 5 things does not get into the array, the output has:


'p1'
'p2'
''
'p4'

is this a bug or something ?

Old Pedant
03-01-2009, 09:48 PM
Really??? You are saying that the length of the array is then only 4???

WOW. I would not have expected that.

I'll try to play with it tomorrow. Can't do it today.

xconspirisist
03-01-2009, 11:21 PM
(edited) Seems normal when you escape it, removing the escapes produces different behaver...

Old Pedant
03-01-2009, 11:30 PM
Yeah, and he *DID* clearly escape it. So why does he get an array with only 4 elements????

milindpatil
03-04-2009, 12:27 PM
yes, stringname.split(delimiter) does not work for examples as yours.
try using stringname.split(delimiter,length)

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String,%20int)

I had the same problem in my code too.

Try this:



String str ="|||0|0|00|||||";
String [] arr = str.split("\\|", (str.length+2));



the extra 2 is to take into consideration the leading and trailing blank in worst case say (|||||||||||). Though obviously no one will use it like that.

shyam
03-04-2009, 08:05 PM
kool i've never noticed that :D