J2ME中的分割字串逻辑 [英] Split string logic in J2ME

查看:49
本文介绍了J2ME中的分割字串逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发J2ME应用程序.

I am developing a J2ME application.

我想在"<br>"&处分割以下字符串逗号:

I want to split the following string at "<br>" & comma:

3,toothpaste,2<br>4,toothbrush,3

我该怎么做?

推荐答案

  private String[] split(String original,String separator) {
    Vector nodes = new Vector();
    // Parse nodes into vector
    int index = original.indexOf(separator);
    while(index >= 0) {
        nodes.addElement( original.substring(0, index) );
        original = original.substring(index+separator.length());
        index = original.indexOf(separator);
    }
    // Get the last node
    nodes.addElement( original );

     // Create split string array
    String[] result = new String[ nodes.size() ];
    if( nodes.size() > 0 ) {
        for(int loop = 0; loop < nodes.size(); loop++)
        {
            result[loop] = (String)nodes.elementAt(loop);
            System.out.println(result[loop]);
        }

    }
   return result;
}

上面的方法将让您围绕传递的分隔符分割字符串,就像J2EE的String.split()一样.因此,首先在换行标记上拆分字符串,然后在返回数组的每个偏移量处添加逗号,". 例如

The above method will let you split a string about the passed separator, much like J2EE's String.split(). So first split the string on the line break tag, and then do it at each offset of the returned array for the "," comma. e.g.

 String[] lines = this.split(myString,"<br>");
 for(int i = 0; i < lines.length; i++) 
 {
      String[] splitStr = this.split(lines[i],",");
      System.out.println(splitStr[0] + " " + splitStr[1] + " " + splitStr[2]);     
 }

这篇关于J2ME中的分割字串逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆