java 如何删除java中字符串之间的\n、\t和空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2461331/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:16:55  来源:igfitidea点击:

How to remove the \n, \t and spaces between the strings in java?

javaregexstring

提问by Praveen

How do you remove the \n, \t and spaces between strings in java?

你如何删除java中字符串之间的\n、\t和空格?

回答by Bozho

str.replaceAll("\s+", "");

\s A whitespace character: [ \t\n\x0B\f\r]

\s 一个空白字符:[ \t\n\x0B\f\r]

java.util.regex.Patternhas all the predefined classes.

java.util.regex.Pattern拥有所有预定义的类。

回答by Mark Byers

Use String.replaceAllwith a regular expression to remove all whitespace:

使用带有正则表达式的String.replaceAll删除所有空格:

s = s.replaceAll("\s+", "");