简体   繁体   中英

StringTokenizer vs. String.split?

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?

If you want split to split on the characters '(' , ')' , ',' , and ' ' , you need to pass a regex that matches any of those. The easiest is to use a character class:

String[] array = a.split("[(), ]");

Normally, parentheses in a regex are a grouping operator and would have to be escaped if you intended them to be used as literals. However, inside the character class delimiters, the parenthesis characters do not have to be escaped.

StringTokenizer does not support regular expressions . The token characters "()," for the StringTokenizer are split , so the StringTokenizer code will split the input when it encounters any one of the following ( or ) or ,

String.split takes a regular expression and parenthesis are used to term different expressions. Since there is nothing in the parenthesis , they are ignored and only the comma , is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM