[[String的trim()
方法]返回一个除去前导和尾随空格的字符串,该字符串还包括换行符('\n'
)。如何在保持换行符的同时获得trim()
功能?
"\n this is new line "
-> "\nthis is new line"
String str = "\n this is new line ";
str = str.replaceAll("\n ", "\n").replaceAll(" $", "");
System.out.println(str);