How to remove newlines from beginning and end of a string (Java)?
问题 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? 回答1: 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(); 回答2: String.replaceAll("[\n\r]", ""); 回答3: tl;dr String cleanString = dirtyString.strip() ; // Call new `String::string` method. String::strip… The old String::trim method