0

The question might look confusing. Here I explain it. I have a single line in a file containing

 "This is the line. containing several sentences .and it is not well formatted."

I want this in sentences according to "." as

This is the line.
containing several sentences.
and it is not well formatted.

I have tried using

 String line = "This is the line. containing several sentences .and it is not well formatted.";
   String[] lineArr = line.split(".");
        for(String sentence:lineArr){
            System.out.println(""+sentence);
        }
    }

My idea is to removing the spaces before and after "." and then applying split(). How should I achieve this?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
vishal munde
  • 333
  • 1
  • 3
  • 13
  • 1
    So how exactly doesn't this work? Include what you are getting and how this differs from what you want. – Arc676 May 30 '16 at 10:18
  • I do not have an idea how to remove spaces before and after a ".". Though I have used mystring.replaceAll("\\s+(?=[.])", ""). It did not worked. – vishal munde May 30 '16 at 10:22
  • 2
    Use this regex: `"\\s*\\.\\s*"`. You can also `.trim()` your sentences. – kongeor May 30 '16 at 10:24
  • You need to start reading documentation of methods you are using. `split` is using regex as argument and in regex `.` has special meaning. It can represent any character (except line separators). To make regex treat it as literal you need to escape it (prefferebly as `split("\\.")`). Also remember that `split` removes delimiters, so you may need to add them in your print statement, or use look-behind `(?<=\\.)` as split parameter. – Pshemo May 30 '16 at 10:24
  • Why duplicate? The question "Split String on dot . as delimiter" is different from what I have asked. It does not remove the spaces. – vishal munde May 30 '16 at 10:25
  • But Thanks for your explanation about the question – vishal munde May 30 '16 at 10:27
  • "Why duplicate?" Because your code example shows that main problem is lack of escaping of dot (unless you posted wrong example) which is explained in duplicate question. I believe that with this information you will be able to solve your problem now (for instance with simple `trim()` method). – Pshemo May 30 '16 at 10:29
  • @Pshemo Thanks. Now I have the idea how to solve it. – vishal munde May 30 '16 at 10:32

0 Answers0