• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Split()-method

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Does anyone has a tested code to split strings on J2ME?
Needed function should be like:
String[] Split(String splitStr, String delimiter)
I do write it by myself, but there is no sense if someone already has it.
Thanks & br,
Jorma
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't a tested implementation, but the task is so trivial that I'd prefer writing my own method than start importing 3rd party libs into my application.

[ September 16, 2003: Message edited by: Lasse Koskela ]
 
Jorma Ikonen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
And thanks about the code. I found that it would work ok, if the argument of delimiter would be char type. Also, the behavior of this is a little bit different as orginal Sun's split implementation. Found main differenccis are:
Sun's split:
str = ",word,word,";
str.split(",");
//above returns array lenght of 3 (empty, word, word)
Your split:
split(",word,word,", ",");
//above returns array lenght of 2 (word, word)
Sun's split:
str = "ABAwordABAwordABA";
str.split("AB");
//above returns array lenght of 3 (empty, Aword, Aword)
Your split:
split("ABAwordABAwordABA", "AB");
//above returns array lenght of 2 (word, word)
Even differencies might sound small, this is very important to notice due the compatibility (J2SE <-> J2ME) issues.
Is there any place, or someone has good ideas where to publish own implementations of J2ME missing functions to reduce workload and do some sense into the situation?
br,
Jorma
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Another try
By the way, shouldn't the latter example

return ( "", "Aword", "Aword", "A" ) instead?
Anyway, here's the new version. It should support the same logic as java.lang.String#split(String, int) in JDK 1.4.1 except for the real regex stuff implemented by java.util.regex.Pattern...

[ September 17, 2003: Message edited by: Lasse Koskela ]
 
Jorma Ikonen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;

}
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...


Yep, I understood. The challenge was just too tempting
I'll try to fix the cases you listed once I get to the machine where the code resides if/when I have time. (I wouldn't want to write the unit test class from scratch...)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • l
  • [/code]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic