Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 split() method
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Martin




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Dec 25, 2005 7:37 pm   Post subject: (No 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.
Martin




PostPosted: Sun Dec 25, 2005 8:23 pm   Post subject: (No subject)

Hey, good idea!

Thanks a ton Smile
Martin




PostPosted: Mon Dec 26, 2005 2:39 am   Post subject: (No subject)

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




PostPosted: 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]
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: