How do I split strings in J2ME?

后端 未结 8 644
無奈伤痛
無奈伤痛 2020-12-01 14:32

How do I split strings in J2ME in an effective way?

There is a StringTokenizer or String.split(String regex) in the standard edition (J2SE), but they are absent in t

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 15:22

    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=sb.length() || start<-1) || str.length()<=0) return index;
        char[] tofind = str.toCharArray();
        outer: for(;start

提交回复
热议问题