How to Split or Tokenize a string in Java

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Filament.io 0 Flares ×

The java.util.StringTokenizer can be used to split a string into individual tokens. As this class is deprecated now, it is better to use the split method of java.lang.String.
Methods
String[] split(String regex) – Splits the string according to the regular expression

 

Example

 

Uses the \\s+ regular expression to split the string into words using space as the delimiter. It splits based on any number of spaces. The + operator is used to evaluate any number of spaces.