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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With