Wednesday, April 27, 2011

Splitting String/Text In J2ME

To split string/text in J2ME you can use these methods.
Include one of these method in your applications


public static String[] split (String a,String delimeter){
String c[]=new String[0];
String b=a;
while (true){
int i=b.indexOf(delimeter);
String d=b;
if (i>=0)
d=b.substring(0,i);
String e[]=new String[c.length+1];
for (int k=0;k
e[k]=c[k];
e[e.length-1]=d;
c=e;
b=b.substring(i+delimeter.length(),b.length());
if (b.length()<=0 || i<0 ) break; } return c; } ----- /** * A method for splitting a string in J2ME. * * @param splitStr The string to split. * @param delimiter The characters to use as delimiters. * @return An array of strings. */ 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] = tokens.elementAt(i); } return splitArray; }

No comments:

Post a Comment

Search This Blog

Total Pageviews