我正在开发一个J2ME应用程序.
我想在"
&逗号分割以下字符串:
"
3,toothpaste,2
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()一样.因此,首先在换行符上拆分字符串,然后在返回数组的每个偏移处为","逗号执行此操作.例如
String[] lines = this.split(myString,"
"); for(int i = 0; i < lines.length; i++) { String[] splitStr = this.split(lines[i],","); System.out.println(splitStr[0] + " " + splitStr[1] + " " + splitStr[2]); }