How do I split strings in J2ME?

后端 未结 8 602
無奈伤痛
無奈伤痛 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:30

    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.

    0 讨论(0)
  • 2020-12-01 15:31

    In J2ME no split, but you can use this code for split.This code works with only 1 simbol delimiter!!! Use NetBeans.File\Create Project\ Java ME\ MobileApplication\Set project name(split)\Set checkmark.Delete all code in your (Midlet.java).Copy this code and past in your (Midlet.java).

    //IDE NetBeans 7.3.1
    //author: UserSuperPupsik 
    //email: usersuperpupsik@gmail.com
    
    
    
    package split;
    
    
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import java.util.Vector;
    
    public class Midlet extends MIDlet {
     public String e1;
        public Vector v=new Vector();
     public int ma;
     int IsD=0;
     int vax=0;
     public String out[];
     private Form f;
    
     public void split0(String text,String delimiter){
                                if (text!=""){
                                IsD=0;
    
                                int raz=0;
    
                                //v.removeAllElements();
                                v.setSize(0);
                                int io;
                                String temp=""; 
                                 int ni=(text.length()-1);
    
    
                                 for(io=0;io<=ni;io++){
    
                                        char ch=text.charAt(io);
                                        String st=""+ch;                                    
                                        if(io==0 && st.equals(delimiter)){IsD=1;}
    
                                        if(!st.equals(delimiter)){temp=temp+st;} //Not equals (!=)
                                        else if(st.equals(delimiter)&&temp!="")//equals (==)
                                                                    {
                                        IsD=1;
                                        //f.append(temp);   
                                        v.addElement(temp);
                                        temp="";                   
    
                                                                     }
    
    
                                         if(io==ni && temp!="") {
                                                  v.addElement(temp);
                                                  temp="";  
                                                  }           
    
                                         if((io==ni)&&IsD==0&&temp!=""){v.addElement(temp);}
    
    
    
    
                                                }
    
    
    
                                           if(v.size()!=0){
    
                                           ma=(v.size());
    
                                           out=new String[ma];
    
    
                                           v.copyInto(out);
    
                                           }
                                           //else if(v.size()==0){IsD=1; }
    
    
                                }
                                     }
    
    
    public void method1(){
        f.append("\n");
        f.append("IsD: " +IsD+"");
        if (v.size()!=0){
        for( vax=0;vax<=ma-1;vax++){
                                    f.append("\n");
    
                                    f.append(out[vax]);
    
    
                                        }
                              }  
    }
        public void startApp() {
    
        f=new Form("Hello J2ME!");
        Display.getDisplay(this).setCurrent(f);
    
        f.append("");
        split0("Hello.World.Good...Luck.end" , ".");
        method1();
    
        split0(".",".");
        method1();
    
        split0("   First WORD2 Word3 "," ");
        method1();
    
        split0("...",".");
        method1();            
                                                    }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    
    
    
    
    }
    

    Splited elements located in array called (out).For Example out[1]:Hello. Good Luck!!!

    0 讨论(0)
提交回复
热议问题