如何解析/拆分字符串?

时间:2015-06-05 07:01:41

标签: java string parsing split

这是我从DB返回的字符串,

"N,591;F\"Vendor Number\",86;F\"Vendor Name\",143;F\"Claim Number\",82;" 

并存储在字符串列中,

我想提取供应商编号'供应商名称'并且'索赔号码&#​​39; ..任何想法我怎么能这样做? 感谢...

2 个答案:

答案 0 :(得分:1)

应该这样做:

String string = "N,591;F\"Vendor Number\",86;F\"Vendor Name\",143;F\"Claim Number\",82;";
String[] s = string.split("\"");
System.out.println(s[1]);
System.out.println(s[3]);
System.out.println(s[5]);

输出:

Vendor Number
Vendor Name
Claim Number

如果列标签的数量未知,请选择每个备用索引。

答案 1 :(得分:1)

包裹测试;

import java.util.ArrayList; import java.util.StringTokenizer;

public class StringSplitTest {

public static void main(String[] args) {

    StringTokenizer st = new StringTokenizer("N,591;F\"Vendor Number\",86;F\"Vendor Name\",143;F\"Claim Number\",82;",";");
    while (st.hasMoreElements()) {
        StringTokenizer notRequired = new StringTokenizer((String) st.nextElement());
        StringTokenizer st1 = new StringTokenizer((String) st.nextElement(),",");
        StringTokenizer st2 = new StringTokenizer((String) st.nextElement(),",");
        StringTokenizer st3 = new StringTokenizer((String) st.nextElement(),",");
        System.out.println(st1.nextElement()+ " = " + st1.nextElement());
        System.out.println(st2.nextElement()+ " = " + st2.nextElement());
        System.out.println(st3.nextElement()+ " = " + st3.nextElement());

}

} }