Error using split method in Codenameone

蹲街弑〆低调 提交于 2019-12-02 06:47:25

问题


I have created a new Codenameone project. It contains the following code:

String values = "one, two, tree";
String[] v = values.split(",");

When I build the project, I got this error:

location: variable definition of type String 
error: cannot find symbol
String[] v = values.split(",");
symbol:   method split(String)

However, if I take the sample project "MapsDemo" and use the split method, everything is ok.

What can be the problem?

Thanks.


回答1:


Codename One supports a subset of Java 5 and String.split() isn't there. Its much harder to change the VM implementation code across all platforms than just add a portable library in the codename one package space. Its also harder to make all the edge cases 100% compatible and it makes the executable larger (you pay for String.split even if you don't use it!).

We have StringUtils and StringTokenizer, there is also a regex package in the cn1lib section.




回答2:


Why don't you try this?

    import java.util.StringTokenizer;
    ... ...
    String fruits = "apple:pear:grape";
    String delimiter = ":";
    StringTokenizer fruitsTokenizer = new StringTokenizer(fruits, delimiter);
    while (fruitsTokenizer.hasMoreTokens()) {
            String fruit = fruitsTokenizer.nextToken();
            //
            // Do here something you want...
            //
    }


来源:https://stackoverflow.com/questions/25875676/error-using-split-method-in-codenameone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!