How to remove newlines from beginning and end of a string?


I have a string that contains some text followed by a blank line. What's the best way to keep the part with text, but remove the whitespace newline from the end?

This question is tagged with java string line blank-line

~ Asked on 2011-09-17 11:17:49

The Best Answer is


Use String.trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.

String trimmedString = myString.trim();

~ Answered on 2011-09-17 11:19:39


String.replaceAll("[\n\r]", "");

~ Answered on 2011-09-17 11:51:41

Most Viewed Questions: