How to split text int array of sentences based on ?, ! and . in Java?
For example I want to store sentences from a string into a oversized array. myArray[0] = 1st sentence, myArray[1] = 2nd sentence and etc/
How to split text int array of sentences based on ?, ! and . in Java?
For example I want to store sentences from a string into a oversized array. myArray[0] = 1st sentence, myArray[1] = 2nd sentence and etc/
You can use String.split(regex)
method, like this:
String[] sentendes = text.split("(?<=[.!?])\\s*");
Using lookbehind should help you preserve the punctuation mark after the sentence.
Here is a small demo on ideone.
Try this regex:
String[] myArray = "sentence! sentence. sentence?".split("(<=[\\!\\?\\.])\\s*")
Explanation:
(<= lookbehind, to preserve punctuation as in @dasblinkenlight's answer
[ start category (which would be !, ?, or .)
\\!\\?\\. punctuation (must be escaped)
] end category
) end lookbehind
\\s* any amount of whitespace