Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

String split comma and parenthisis-JAVA

Convert the string: "(id,created,employee(id,firstname,employeeType(id), lastname),location)" to the following output id created employee - id - firstname - employeeType -- id - lastname location

is there any optimistic solution available with using regular expression

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
public static void main(String[] args) {
    String a = "(id,created,employee(id,firstname," + 
             "employeeType(id), lastname),location)";
    StringTokenizer tok = new StringTokenizer(a, "(), ");
    System.out.println("StringTokenizer example");
    while (tok.hasMoreElements()) {
        String b = (String)tok.nextElement();
        System.out.println(b);
    }
    System.out.println("Split example");
    String[] array = a.split("(),");
    for (String ii: array) {
         System.out.println(ii);
    }

 } 
}

Outputs

StringTokenizer example
id
created
employee
id
firstname
employeeType
id
lastname
location
Split example
(id
created
employee(id
firstname
employeeType(id)
lastname)
location)

You can add the hyphens.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...