Split string logic in J2ME

前端 未结 2 898
無奈伤痛
無奈伤痛 2021-01-11 23:23

I am developing a J2ME application.

I want to split the following string at \"
\"
& comma:

3,toothpaste,2
4,toothb
2条回答
  •  礼貌的吻别
    2021-01-11 23:43

      private String[] split(String original,String separator) {
        Vector nodes = new Vector();
        // Parse nodes into vector
        int index = original.indexOf(separator);
        while(index >= 0) {
            nodes.addElement( original.substring(0, index) );
            original = original.substring(index+separator.length());
            index = original.indexOf(separator);
        }
        // Get the last node
        nodes.addElement( original );
    
         // Create split string array
        String[] result = new String[ nodes.size() ];
        if( nodes.size() > 0 ) {
            for(int loop = 0; loop < nodes.size(); loop++)
            {
                result[loop] = (String)nodes.elementAt(loop);
                System.out.println(result[loop]);
            }
    
        }
       return result;
    }
    

    The above method will let you split a string about the passed separator, much like J2EE's String.split(). So first split the string on the line break tag, and then do it at each offset of the returned array for the "," comma. e.g.

     String[] lines = this.split(myString,"
    "); for(int i = 0; i < lines.length; i++) { String[] splitStr = this.split(lines[i],","); System.out.println(splitStr[0] + " " + splitStr[1] + " " + splitStr[2]); }

提交回复
热议问题