2

How can i split a sentence what is plotted with space and , in same time

E.g

String Sentence="Please , tell me about : job, hobby";

String[] words = Sentence.split(" ");<--- so here i need ',' ':' ' ' splits, How can i resolve multiple splitting?

Norbert
  • 21
  • 2
  • 2
    Duplicate of http://stackoverflow.com/questions/7492672/java-string-split-by-multiple-character-delimiter – kai Sep 16 '15 at 09:24

1 Answers1

4

You can use a non-word command and a quantifier:

String sentence="Please , tell me about : job, hobby";
System.out.println(Arrays.toString(sentence.split("\\W+")));

Output

[Please, tell, me, about, job, hobby]

You can also use a character class with the specific tokens you need:

System.out.println(Arrays.toString(sentence.split("[,: ]+")));

(same output)

Docs

Here.

Mena
  • 47,782
  • 11
  • 87
  • 106