How to parse/split a string?

75 views Asked by At

This is my string which is returned from DB,

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

and stored in string columns,

I want to extract 'Vendor Number','Vendor Name' and 'Claim Number'..Any ideas how I can do that? Thanks...

2

There are 2 answers

0
Satyajeet Kanetkar On BEST ANSWER

This ought to do it:

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]);

Output:

Vendor Number
Vendor Name
Claim Number

If the number of column labels is not known, choose every alternate index.

0
Akshada On

package test;

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());

}

} }