开发者

How to split a string with java but also keep the delimiters end of sentance [duplicate]

This question already exists: 开发者_StackOverflow社区 Closed 11 years ago.

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜