0

How to split the text in to sentence based on set of characters as delimiter ?. I have a function where user will input text and the delimiter. Delimiter can be single character or a string (user can specify 'xyz' as a delimiter). I have tried StringTokenizer(text, delimiter) which takes only character as delimiter. Next option was String.split() but this fails when I give delimiter such as '**'. It requires "\" to be appended to the delimiter. Appending "\" looks like very tedious job because if the delimiter is "**" then I have to change it to "\*\*". If the delimiter is say "< TAB >" then I split function looks like String [] sentences = text.split("\\<TAB\\>");

Is there any easy way to split text in to sentences based of set of characters as delimiter ?.

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
Hegde
  • 451
  • 1
  • 8
  • 16
  • `StringTokenizer(String, String)` doesn't only accept a single character as a delimiter, it accepts a `String`. – Bobulous May 17 '15 at 10:46

1 Answers1

2

I think you could easily google this, yet I will provide you the answer:

String separator = ...
s.split(Pattern.quote(separator));

The answer comes directly from here.

Community
  • 1
  • 1
Dawid Bugajewski
  • 353
  • 1
  • 10