How to reformat paragraph to have each sentence on a separate line?
Input:
Hi. I am John.
My name is John. Who are you ?
O开发者_运维百科utput:
Hi
I am John
My name is John
Who are you
String line = "Hi. My name is John. Who are you ?";
String[] sentences = line.split("(?<=[.!?])\\s+");
for (String sentence : sentences) {
System.out.println("[" + sentence + "]");
}
This produces:
[Hi.]
[My name is John.]
[Who are you ?]
See also
- regular-expressions.info tutorials
- Lookarounds
- Character classes
- Java language guide: the for-each loop
If you're not comfortable using split
(even though it's the recommended replacement for the "legacy" java.util.StringTokenizer
), you can just use only java.util.Scanner
(which is more than adequate to do the job).
See also
- Scanner vs. StringTokenizer vs. String.Split
Here's a solution that uses Scanner
, which by the way implements Iterator<String>
. For extra instructional value, I'm also showing an example of using java.lang.Iterable<T>
so that you can use the for-each construct.
final String text =
"Hi. I am John.\n" +
"My name is John. Who are you ?";
Iterable<String> sentences = new Iterable<String>() {
@Override public Iterator<String> iterator() {
return new Scanner(text).useDelimiter("\\s*[.!?]\\s*");
}
};
for (String sentence : sentences) {
System.out.println("[" + sentence + "]");
}
This prints:
[Hi]
[I am John]
[My name is John]
[Who are you]
If this regex is still not what you want, then I recommend investing the time to educate yourself so you can take matters into your own hand.
See also
- What is the Iterable interface used for?
- Why is Java’s Iterator not an Iterable?
Note: the final
modifier for the local variable text
in the above snippet is a necessity. In an illustrative example, it makes for a concise code, but in your actual code you should refactor the anonymous class to its own named class and have it take text
in the constructor.
See also
- Anonymous vs named inner classes? - best practices?
- Cannot refer to a non-final variable inside an inner class defined in a different method
精彩评论