Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Copy edited.
Source Link
Peter Mortensen

Please don't use StringTokenizer class as it is a legacy class that is retained for compatibility reasons although, and its use is discouraged in new code. And we can make use of the split method as suggested by others as well.

String[] sampleTokens = "004-034556".split("-");
System.out.println(Arrays.toString(sampleTokens));

andAnd as expected it will print: [004, 034556]

[004, 034556]

In this answer I also want to point out one change that has taken place for split method in Java 8. The String#split() method makes use of Pattern.split, and now it will remove empty strings at the start of the result array. Notice this change in docdocumentation for Java 8:

When there is a positive-width match at the beginning of the input sequence then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.

It means for the following example:

String[] sampleTokensAgain = "004".split("");
System.out.println(Arrays.toString(sampleTokensAgain));

we will get three strings: [0, 0, 4] and not four as was the case in Java 7 and before. Also check this similar question.

Please don't use StringTokenizer class as it is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. And we can make use of split method as suggested by others as well.

String[] sampleTokens = "004-034556".split("-");
System.out.println(Arrays.toString(sampleTokens));

and as expected it will print: [004, 034556]

In this answer I also want to point out one change that has taken place for split method in Java 8. The String#split() method makes use of Pattern.split and now it will remove empty strings at the start of result array. Notice this change in doc for Java 8:

When there is a positive-width match at the beginning of the input sequence then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.

It means for the following example:

String[] sampleTokensAgain = "004".split("");
System.out.println(Arrays.toString(sampleTokensAgain));

we will get three strings: [0, 0, 4] and not four as was the case in Java 7 and before. Also check this similar question.

Please don't use StringTokenizer class as it is a legacy class that is retained for compatibility reasons, and its use is discouraged in new code. And we can make use of the split method as suggested by others as well.

String[] sampleTokens = "004-034556".split("-");
System.out.println(Arrays.toString(sampleTokens));

And as expected it will print:

[004, 034556]

In this answer I also want to point out one change that has taken place for split method in Java 8. The String#split() method makes use of Pattern.split, and now it will remove empty strings at the start of the result array. Notice this change in documentation for Java 8:

When there is a positive-width match at the beginning of the input sequence then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.

It means for the following example:

String[] sampleTokensAgain = "004".split("");
System.out.println(Arrays.toString(sampleTokensAgain));

we will get three strings: [0, 0, 4] and not four as was the case in Java 7 and before. Also check this similar question.

Source Link
akhil_mittal

Please don't use StringTokenizer class as it is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. And we can make use of split method as suggested by others as well.

String[] sampleTokens = "004-034556".split("-");
System.out.println(Arrays.toString(sampleTokens));

and as expected it will print: [004, 034556]

In this answer I also want to point out one change that has taken place for split method in Java 8. The String#split() method makes use of Pattern.split and now it will remove empty strings at the start of result array. Notice this change in doc for Java 8:

When there is a positive-width match at the beginning of the input sequence then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.

It means for the following example:

String[] sampleTokensAgain = "004".split("");
System.out.println(Arrays.toString(sampleTokensAgain));

we will get three strings: [0, 0, 4] and not four as was the case in Java 7 and before. Also check this similar question.

lang-java