Rabindra
Rabindra

Reputation: 101

How to split the string after dot and print it to next line

String string = "This is a example.just to verify.please help me."; 
if(string.matches("(.*).(.*)"))
{
  System.out.println(true); 
  String[] parts = string.split("\\r?\\n");
    for(String part:parts){
       System.out.println(part);
    }
}

I want to split the string after every dot to the next line. can anyone help me in this. thanks in advance.

Upvotes: 0

Views: 134

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174766

Use positive lookbehind. And also in matches function, you need to escape the dot like string.matches(".*\\..*"), since dot is a regex special character which matches any character.

String[] parts = string.split("(?<=\\.)");

or

If you don't want to do a split after the last dot.

String[] parts = string.split("(?<=\\.)(?!$)");

DEMO

Upvotes: 2

Ankur Singhal
Ankur Singhal

Reputation: 26077

use regex "\\."

public static void main(String[] args) {
        String string = "This is a example.just to verify.please help me.";
        if (string.matches("(.*).(.*)")) {
            System.out.println(true);
            String[] parts = string.split("\\.");
            for (String part : parts) {
                System.out.println(part);
            }
        }
    }

output

true
This is a example
just to verify
please help me

Upvotes: 2

Related Questions