How to split a string with java but also keep the delimiters end of sentance [duplicate]
Possible Duplicate:
Is there a way to split strings with String.split() and include the delimiters?
Suppose I have the following sentences-
What is your name? My name is Don. You are so nice!
I want the output by java as follows
What is your name?
My name is Don.
You are so nice!
I used the java split() method. but it split without the delimiters. i used split("[\\.!?]")
This does the trick:
split("(?<=[.?!])");
(Adapted from this great answer)
You should split on whitespace, with a lookbehind on the '.!?' characters.
String s = "What is your name? My name is Don. You are so nice!";
String[] tokens = s.split("(?<=[\\.\\!\\?])\\s");
for (String t : tokens) System.out.println(t);
精彩评论