Hi Lasse,
And thanks again about the code. The current version works better, but it still has some differencies compared to the orginal implementation of split-function on J2SE.
Found differencies are:
Case 1
splitStr = "";
delimiter ",";
Sun split returns array lenght of 1 ("empty")
Your returns array lenght of 0
Case 2
splitStr = ",,,";
delimiter "";
Sun split returns array lenght of 4 ("empty" "," "," ",")
Your returns array lenght of 1 (",,,")
Case 3
splitStr = ",,A,";
delimiter ",";
Sun split returns array lenght of 4 ("empty" "empty" "empty" "A")
Your returns array lenght of 5 ("empty" "empty" "empty" "A" "empty")
Case 4
splitStr = null;
delimiter ",";
Sun split returns NullPointerException
Your returns array lenght of 0
Case 5
splitStr = ",,,";
delimiter null;
Sun split returns NullPointerException
Your returns array lenght of 1 (",,,")
BTW, my aim was just to ask if someone has already made the split-function to J2ME, not to but people to do my work... Anyway, here is my version of split-function what seems to work similar as orginal J2SE, but do not has regex capability. I tried to
test it as well I could, but I can't give any guarantee for it.
I think that it's quite much matter of opinion which one (mine or Lasse's) you are going to use. Lasse's solution seems to have more logical behavior, but it's not 100% compatible to J2SE. Have a nice day!
br,
Jorma
Split (shit) Code:
static String[] split(String splitStr, String delimiter){
/** Note, delimiter can't be regex-type of argument as
* in orginal J2SE implementation!!!*/
int dLen = delimiter.length();
int p1 = 0;
int cnt = 0;
if (splitStr.length() == 0){
String[] excepStr = new String[1];
excepStr[0] = "";
return excepStr;
}
if (dLen == 0){
String[] excepStr = new String[splitStr.length()+1];
excepStr[0] = "";
for (int i = 0; i<excepStr.length-1; i++){
excepStr[i+1] = String.valueOf(splitStr.charAt(i));
}
return excepStr;
}
p1 = splitStr.indexOf(delimiter, p1);
while (p1 != -1){
cnt++;
p1 = p1 + dLen;
p1 = splitStr.indexOf(delimiter, p1);
}
String[] tmp = new String[cnt + 1];
p1 = 0;
int p2 = 0;
for (int i = 0; i<tmp.length; i++){
p2 = splitStr.indexOf(delimiter, p2);
if (p2 == -1){
tmp[i] = splitStr.substring(p1);
}else{
tmp[i] = splitStr.substring(p1, p2);
}
p1 = p2 + dLen;
p2 = p2 + dLen;
}
cnt = 0;
for (int i = tmp.length-1; i>-1; i--){
if(tmp[i].length() > 0){
break;
} else{
cnt++;
}
}
String[] result = new String[tmp.length-cnt];
for (int i = 0; i<result.length; i++){
result[i] = tmp[i];
}
return result;
}