John
John

Reputation: 4533

How to reformat paragraph to have each sentence on a separate line?

Input:

Hi. I am John.
My name is John. Who are you ?

Output:

Hi
I am John
My name is John
Who are you

Upvotes: 3

Views: 978

Answers (1)

polygenelubricants
polygenelubricants

Reputation: 383746

    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


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

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


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

Upvotes: 6

Related Questions