我正在尝试在文本框中取出字符串,然后从文本框中拆分文本并将其显示在另一个表单上,但我无法实现它,因为每次我放置拆分功能时它会显示错误 这是代码
if (c == next) {
String str = tb.getString();
String[] st = str.split(":|;")
System.out.println(st);
f1.append(str);
display.setCurrent(f1);
}
答案 0 :(得分:2)
J2ME不支持分割方法。
答案 1 :(得分:1)
您错过了 ;
if (c == next) {
String str = tb.getString();
String[] st = str.split(":|;");
System.out.println(st);
f1.append(str);
display.setCurrent(f1);
}
答案 2 :(得分:0)
/**
* Splits a string into multiple strings
*
* @param separator Separator char
* @param source_string Source string
* @return Array of strings
*
* source :
* http://www.particle.kth.se/~lindsey/JavaCourse/Book/Code/P3/Chapter24/SNAP/Worker.java
*/
public static String[] split(char separator, String source_string) {
// First get rid of whitespace at start and end of the string
String string = source_string.trim();
// If string contains no tokens, return a zero length array.
if (string.length() == 0) {
return (new String[0]);
}
// Use a Vector to collect the unknown number of tokens.
Vector token_vector = new Vector();
String token;
int index_a = 0;
int index_b;
// Then scan through the string for the tokens.
while (true) {
index_b = string.indexOf(separator, index_a);
if (index_b == -1) {
token = string.substring(index_a);
token_vector.addElement(token);
break;
}
token = string.substring(index_a, index_b);
token_vector.addElement(token);
index_a = index_b + 1;
}
return toStringArray(token_vector);
} // split
/**
* Convert a vector to an array of string
*
* @param vector Vector of string
* @return Array of string
*/
public static String[] toStringArray(Vector vector) {
String[] strArray = new String[vector.size()];
for (int i = 0; i < strArray.length; i++) {
strArray[i] = (String) (vector.elementAt(i));
}
return strArray;
}