views:

1730

answers:

7

How do I split strings in J2ME in an effective way? There is a StringTokenizer in the standard edition, but it is absent in the micro edition.

+1  A: 

There is no built in method to split strings. You have to write it on your own using String.indexOf() and String.substring(). Not hard.

Guido
Yes, but will it be memory effective? String.substring() will create new strings, which will hog the memory until garbage collected.
Spoike
StringTokenizer.nextToken() contains "return string.subString(...)" internally so I don't think you're any worse off...
Cowan
Substring uses a private constructof for String which shares the (immutable) char array, so yes - it will be memory efficient.
ddimitrov
A: 

String.split isn't available?

There is one in J2SE.

Zarkonnen
Nope, you're thinking about C#. There is no String.split(...) in Java.
Spoike
There is in Java, just not J2ME.
Bill the Lizard
Why the heck would they leave it out of J2ME?
matt b
Not thinking C#. See edited comment above.
Zarkonnen
A: 

That depends on what exactly you want to achieve, but the function String.substring() will be in there somewhere:

String myString = "Hello World";

// This will print the substring starting from index 6 to the end of the string

System.out.println(myString.substring(6));

// This will print the substring starting from index 0 until index 5

System.out.println(myString.substring(0,5));

// Output

World

Hello

Combine this with the other String functions (indexOf() etc.) to achieve the desired effect!

Re-reading your question, it looks as though you may have been looking for String.split(). This will split your input string into an array of strings based on a given regex:

String myString = "Hi-There-Gang";

String[] splitStrings = myString.split("-");

This will result in the splitStrings array containing three string, "Hi", "There" and "Gang".

Re-reading your question again, String.split is not available in J2ME, but the same effect can be achieved with substring and indexOf.

Rob Bell
+2  A: 

String.split(...) is available in J2SE, but not J2ME.
You are required to write your own algorithm: related post with sample solution.

vanslly
A: 

Does this discussion help? (found googling for "Java String split me"). There's code posted in the thread.

Olaf
Is downvoting for the google reference? If so I apologize for sounding smart-ass. It was rather meant as helping to think about what question to ask google in order to find... If the code found there is wrong (it's a lot more than the accepted answer) - too bad. anyway...
Olaf
+1  A: 

There are a few implementations of a StringTokenizer class for J2ME. This one by Ostermiller will most likely include the functionality you need

See also this page on Mobile Programming Pit Stop for some modifications and the following example:

String firstToken;
StringTokenizer tok;

tok = new StringTokenizer("some|random|data","|");
firstToken= tok.nextToken();
Jonas Pegerfalk
Thanks, that was exactly what I needed.
Spoike
A: 

I hope this one will help you... This is my own implementation i used in my application. Of course this can still be optimized. i just do not have time to do it... and also, I am working on StringBuffer here. Just refactor this to be able to use String instead.

public static String[] split(StringBuffer sb, String splitter){
    String[] strs = new String[sb.length()];
    int splitterLength = splitter.length();
    int initialIndex = 0;
    int indexOfSplitter = indexOf(sb, splitter, initialIndex);
    int count = 0;
    if(-1==indexOfSplitter) return new String[]{sb.toString()};
    while(-1!=indexOfSplitter){
        char[] chars = new char[indexOfSplitter-initialIndex];
        sb.getChars(initialIndex, indexOfSplitter, chars, 0);
        initialIndex = indexOfSplitter+splitterLength;
        indexOfSplitter = indexOf(sb, splitter, indexOfSplitter+1);
        strs[count] = new String(chars);
        count++;
    }
    // get the remaining chars.
    if(initialIndex+splitterLength<=sb.length()){
        char[] chars = new char[sb.length()-initialIndex];
        sb.getChars(initialIndex, sb.length(), chars, 0);
        strs[count] = new String(chars);
        count++;
    }
    String[] result = new String[count];
    for(int i = 0; i<count; i++){
        result[i] = strs[i];
    }
    return result;
}

public static int indexOf(StringBuffer sb, String str, int start){
    int index = -1;
    if((start>=sb.length() || start<-1) || str.length()<=0) return index;
    char[] tofind = str.toCharArray();
    outer: for(;start<sb.length(); start++){
        char c = sb.charAt(start);
        if(c==tofind[0]){
            if(1==tofind.length) return start;
            inner: for(int i = 1; i<tofind.length;i++){ // start on the 2nd character
                char find = tofind[i];
                int currentSourceIndex = start+i;
                if(currentSourceIndex<sb.length()){
                    char source = sb.charAt(start+i);
                    if(find==source){
                        if(i==tofind.length-1){
                            return start;
                        }
                        continue inner;
                    } else {
                        start++;
                        continue outer;
                    }
                } else {
                    return -1;
                }

            }
        }
    }
    return index;
}
demotics2002
btw, here's how you can use the code...String[] elements = split(new StringBuffer("Hello|World"), "|");or String[] elements = split(new StringBuffer("HelloblahWorld"), "blah");both returns {"Hello", "World"}
demotics2002