保留换行符时的修剪字符串

问题描述 投票:0回答:1

[[String的trim()方法]返回一个除去前导和尾随空格的字符串,该字符串还包括换行符('\n')。如何在保持换行符的同时获得trim()功能?

例如:"\n this is new line "-> "\nthis is new line"
java regex string kotlin removing-whitespace
1个回答
0
投票
您可以改用replaceAll

String str = "\n this is new line "; str = str.replaceAll("\n ", "\n").replaceAll(" $", ""); System.out.println(str);

© www.soinside.com 2019 - 2024. All rights reserved.