Tokenizing a String in Java


We have the following string −

String str = "This is demo text, and demo line!";

To tokenize the string, let us split them after every period (.) and comma (,)

String str = "This is demo text, and demo line!";

The following is the complete example.

Example

 Live Demo

public class Demo {
    public static void main(String[] args) {
       String str = "This is demo text, and demo line!";
       String[] res = str.split("[, .]", 0);
       for(String myStr: res) {
          System.out.println(myStr);
       }
    }
}

Output

This
is
demo
text

and
demo
line!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements