java 如何修剪字符串中的“输入键”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5090483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to trim "enter key" in a string
提问by Learner
String strFCKeditor1 = request.getParameter("FCKeditor1");
String strFCKeditor1 = request.getParameter("FCKeditor1");
回答by Snake
String strFCKeditor1 = request.getParameter("FCKeditor1").replaceAll("\r|\n", "");
You also need to replace the \r
and/or \n\r
(hence the regex), not just \n
as stated in the other answer, since you don't know whether your user is using Windows / Mac OSX / Linux.
您还需要替换\r
和/或\n\r
(因此是正则表达式),而不仅仅是\n
如其他答案中所述,因为您不知道您的用户是否使用 Windows/Mac OSX/Linux。
回答by Jigar Joshi
strFCKeditor1.replaceAll("\n","");//this will replace all \n with blank string
回答by Pushkar
From your question i assume you want to remove the newline characters from the end of your string.
从您的问题中,我假设您想从字符串的末尾删除换行符。
Use -
利用 -
StringUtils.chomp(str);
StringUtils.chomp(str);
http://commons.apache.org/lang/apidocs/src-html/org/apache/commons/lang3/StringUtils.html#line.4353
http://commons.apache.org/lang/apidocs/src-html/org/apache/commons/lang3/StringUtils.html#line.4353