Computer Science Canada

split() method

Author:  Martin [ Sun Dec 25, 2005 7:18 pm ]
Post subject:  split() method

code:

String ts = ",,,asdf,asdf,,,asdf,,adsf,,fdsa,,,,,fdas,,fsda,,,fin,,,";
System.out.println ("test string = " + ts);
String[] tss = ts.split(",");
for (int i = 0; i < tss.length; ++i) {
    System.out.println (tss[i]);
}
System.out.println ("done");


Now, tss in this case is "", "", "", "asdf", ..., "fin" with the last three columns ignored. Does anyone know how I can get it to include these?

Author:  wtd [ Sun Dec 25, 2005 7:37 pm ]
Post subject: 

Brute force, but add a single character to the end, if the last character is a comma. Then later just ignore the last String in the array.

Author:  Martin [ Sun Dec 25, 2005 8:23 pm ]
Post subject: 

Hey, good idea!

Thanks a ton Smile

Author:  Martin [ Mon Dec 26, 2005 2:39 am ]
Post subject: 

Apparently if you call the split method with somestring.split(regex, some_negative_number) it goes to the end of the string.

Author:  MysticVegeta [ Thu Dec 29, 2005 12:23 am ]
Post subject:  Re: split() method

Martin wrote:
code:

String ts = ",,,asdf,asdf,,,asdf,,adsf,,fdsa,,,,,fdas,,fsda,,,fin,,,";
System.out.println ("test string = " + ts);
String[] tss = ts.split(",");
for (int i = 0; i < tss.length; ++i) {
    System.out.println (tss[i]);
}
System.out.println ("done");


Now, tss in this case is "", "", "", "asdf", ..., "fin" with the last three columns ignored. Does anyone know how I can get it to include these?


um could you run a backwards loop and check for it then?

[syntax="Turing Pseudo *untested*"]
var endString : string
for decreasing x : length(ts)..1
if ts(x) not= "," then
endString := ts(x+1..*)
ts := ts(1..x)
exit
end if
end for
put stringSplit (ts, ",")+endString
[/syntax]


: