繁体   English   中英

Java拆分功能在j2me中不起作用

[英]Java split function not woring in j2me

我正在尝试在文本框中输入字符串,然后从文本框中拆分文本并以另一种形式显示它,但是我无法实现它,因为每次我将split函数显示为错误时,这是​​代码

if (c == next) {
  String str = tb.getString();
  String[] st = str.split(":|;")
  System.out.println(st);

  f1.append(str);

  display.setCurrent(f1);
}

J2ME不支持拆分方法。

你错过了;

if (c == next) {
  String str = tb.getString();
  String[] st = str.split(":|;");
  System.out.println(st);

  f1.append(str);

  display.setCurrent(f1);
}
/**
 * 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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