我正在开发一个J2ME应用程序。
我想在"<br>"
&amp;逗号:
3,toothpaste,2<br>4,toothbrush,3
我该怎么做?
答案 0 :(得分:18)
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()一样。因此,首先在换行符上拆分字符串,然后在返回数组的每个偏移处为“,”逗号执行此操作。 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]);
}
答案 1 :(得分:1)
private String[] split(String original, String separator, int count)
{
String[] result;
int index = original.indexOf(separator);
if(index >= 0)
result = split(original.substring(index + separator.length()), separator, count + 1);
else
{
result = new String[count + 1];
index = original.length();
}
result[count] = original.substring(0, index);
return result;
}
String[] lines = this.split(myString,"<br>",0);