简体   繁体   中英

Split text in J2ME

I'm creating an application that is supposed to read text from mySql database using a get method.

Once it gets data elements from the database as string, it's supposed to split the string and create a list using the string however the split() method does not seem to work here.

J2ME says cannot find method split() - what should I do?

My code is below:

/* assuming the string (String dataString) has already
been read from the database and equals one,two three
i.e String dataString = "one,two,three"; */

String dataArray[];
String delimiter = ",";
dataArray = dataString.split(delimiter);

//continue and create a list from the array.

I've tried this on a desktop and console application and seems to work perfectly, but code does not run in a j2me application. Is there a method that I'm supposed to use? What can I do?

Here is a high speed implementation:

 public static String[] Split(String splitStr, String delimiter) {
     StringBuffer token = new StringBuffer();
     Vector tokens = new Vector();
     // split
     char[] chars = splitStr.toCharArray();
     for (int i=0; i < chars.length; i++) {
         if (delimiter.indexOf(chars[i]) != -1) {
             // we bumbed into a delimiter
             if (token.length() > 0) {
                 tokens.addElement(token.toString());
                 token.setLength(0);
             }
         } else {
             token.append(chars[i]);
         }
     }
     // don't forget the "tail"...
     if (token.length() > 0) {
         tokens.addElement(token.toString());
     }
     // convert the vector into an array
     String[] splitArray = new String[tokens.size()];
     for (int i=0; i < splitArray.length; i++) {
         splitArray[i] = (String)tokens.elementAt(i);
     }
     return splitArray;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM