-2

I get file's content as a String:

Test1 2.00 04/03/2016
Test2 2.00 04/03/2016
Test3 2.00 04/03/2016

     END

Just imagine that END is not there. I had to put it so that stackoverflow doesn't trim the blank line at the end on its own. Anyway, what would be the best way to quickly remove these blank lines just from the end of the file? Any blank lines in the middle of start of the file have to persist. Any blank spaces or tabs to the right of the text lines also have to persist.

goe
  • 1,153
  • 2
  • 12
  • 24

1 Answers1

5

s = s.replaceAll("([\\n\\r]+\\s*)*$", "");

As requested, this will preserve:

  • empty/whitespace lines at beginning
  • empty/whitespace lines in middle
  • trailing whitespace on any non-empty line

It will remove:

  • empty/whitespace lines at end
The111
  • 5,757
  • 4
  • 39
  • 55