Someone just asked a question on String.split() and the solution was to use StringTokenizer. String split comma and parenthisis-JAVA Why doesn't String.split() split on parentheses?
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)
There was a discussion on String.split() vs. StringTokenizer at Scanner vs. StringTokenizer vs. String.Split but it doesn't explain the parentheses. Is this by design? What's going on here?