This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Split()-method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Split()-method" Watch "Split()-method" New topic
Author

Split()-method

Jorma Ikonen
Ranch Hand

Joined: Sep 17, 2001
Posts: 49
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
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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 ]

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Jorma Ikonen
Ranch Hand

Joined: Sep 17, 2001
Posts: 49
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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,%20int&41;" target="_blank" rel="nofollow">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

Joined: Sep 17, 2001
Posts: 49
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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...)
elango boopathy
Greenhorn

Joined: Oct 13, 2009
Posts: 1
  • l
  • [/code]
     
    GeeCON Prague 2014
     
    subject: Split()-method