-3

To split a paragraph into an array of individual words (say a string array), the most common answer would be something like:

String para = "ameya needs to win this cup.";
String[] abc = para.split(" ");

However if the para included ? and ,'s and ; etc, how can this be done? For eg:

String para = "ameya,needs. to win?this cup.";
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132

2 Answers2

1

String#split(arg) takes regex as argument, and in regex if you want to match one of many characters then you can use this form (a|b|c|...|z) which means character that is eater a OR b OR c OR [...] OR z (instead of ... you actually need to put rest of alphabet letters).

But since that form is ugly you can use character class that can look like [abcd...z]. But this can also be optimized a little using range of characters [a-z].

Now lets go back to your question. If you want to match all spaces and additional characters then you can try to split on every [\\s.,;?]. Also in case you want to split on single group of that characters you can use [\\s.,;?]+. + means one or more elements that are described before +.

So maybe try this way

String[] abc = para.split("[\\s.,;?]+");
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

Use a regular expression

String str = "ameya,needs. to win?this cup.";
String [] arr = str.split("[\\s|,|\\.|;|\\?]");
ug_
  • 11,267
  • 2
  • 35
  • 52
  • Same: this is a character class – fge Jun 16 '13 at 18:52
  • What fge meant to say is that you don't need to use `|` inside `[...]` to say OR. Right now you are including `|` into your character class so it will also match every `|` in data string. – Pshemo Jun 16 '13 at 19:16