|
Author |
Topic: a JDK 1.1 friendly String.split()... (Read 1288 times) |
|
amoeba
|
a JDK 1.1 friendly String.split()...
« on: Dec 18th, 2003, 4:34pm » |
|
just discovered that the java.lang.String.split() method is JDK 1.4 only. here is a homemade JDK 1.1 friendly replacement for those of you who need to run in browsers. it assumes that you expect words to be separated by spaces only. oh, just realized: only single spaces allowed! you've been warned. I guess using StringTokenizer would've been more robust, but I just hacked this up... Code:String[] split(String s) { int cnt=0,index; String parsed[],tmp; // Strip trailing spaces... while(s.endsWith(" ")) s=s.substring(0,s.length()-1); // Find number of words in string for(int i=0; i<s.length(); i++) if(s.charAt(i)==' ') cnt++; parsed=new String[cnt+1]; tmp=s; for(int i=0; i<cnt+1; i++) { index=tmp.indexOf(" "); if(index!=-1) { parsed[i]=tmp.substring(0,index); tmp=tmp.substring(index+1); } else parsed[i]=tmp; } return parsed; } |
|
|
« Last Edit: Dec 18th, 2003, 4:37pm by amoeba » |
|
marius watz // amoeba http://processing.unlekker.net/
|
|
|
amoeba
|
Re: a JDK 1.1 friendly String.split()...
« Reply #1 on: Feb 21st, 2004, 11:34pm » |
|
updated version with specifiable separator: Code:String[] split(String s,char separator) { int cnt=0,index; String parsed[],tmp; // Strip trailing separators... while(s.endsWith(separator)) s=s.substring(0,s.length()-1); // Find number of words in string for(int i=0; i<s.length(); i++) if(s.charAt(i)==separator) cnt++; parsed=new String[cnt+1]; tmp=s; for(int i=0; i<cnt+1; i++) { index=tmp.indexOf(separator); if(index!=-1) { parsed[i]=tmp.substring(0,index); tmp=tmp.substring(index+1); } else parsed[i]=tmp; } return parsed; } |
|
|
marius watz // amoeba http://processing.unlekker.net/
|
|
|
fry
|
Re: a JDK 1.1 friendly String.split()...
« Reply #2 on: Feb 23rd, 2004, 2:06am » |
|
this is also why splitStrings() exists in the p5 api.
|
|
|
|
amoeba
|
Re: a JDK 1.1 friendly String.split()...
« Reply #3 on: Feb 23rd, 2004, 10:02am » |
|
you know, now that I think about it, that makes a lot of sense... typical, I never even looked for it.
|
marius watz // amoeba http://processing.unlekker.net/
|
|
|
jen
|
Re: a JDK 1.1 friendly String.split()...
« Reply #4 on: Apr 1st, 2004, 7:08am » |
|
hello I cant seem to get splitStrings() to work this is the line String[] dataBlock = nmea.splitStrings(); and I get this error no method named 'splitStrings" was found in type 'java.lang.String" what am I doing wrong?
|
|
|
|
Bijeoma
|
Re: a JDK 1.1 friendly String.split()...
« Reply #5 on: Apr 1st, 2004, 7:27am » |
|
you used incorrect syntax. the correct syntax is. String[] dataBlock = splitStrings(nmea);
|
|
|
|
jen
|
Re: a JDK 1.1 friendly String.split()...
« Reply #6 on: Apr 1st, 2004, 8:19am » |
|
cheers
|
|
|
|
fry
|
Re: a JDK 1.1 friendly String.split()...
« Reply #7 on: Apr 2nd, 2004, 5:30pm » |
|
fyi.. in rev 69 this is now just called 'split()'. so the syntax would be: String[] dataBlock = split(nmea);
|
|
|
|
|