I am going to add an answer to this question because, although I had the same question, the answer provided was not enough. Given some thoughts, I realized that this can be done very easily with regular expression.
To remove new lines from the beginning:
and end of line:
// Trim right String z = "\n\nfrom the end\n\n"; System.out.println("-" + z.split("\\n+$", 2)[0] + "-");
I am sure this is not the most efficient way to trim a string. But this is apparently the cleanest and easiest way to embed such an operation.
Note that the same method can be performed to trim any variations and combination of characters from either end, as this is a simple regular expression.
Zhro Mar 06 '17 at 7:14 2017-03-06 07:14
source share