string 从 Groovy 中的字符串中删除空格

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

Removing whitespace from strings in Groovy

stringgroovy

提问by monda

I have a string like

我有一个像

String  str = "My name is Monda"

How can I achieve a string like

我怎样才能实现像这样的字符串

str  = "MynameisMonda"

回答by geo

You can use the replaceAll()function.

您可以使用该replaceAll()功能。

For your case:

对于您的情况:

replaceAll("\s","")

where \smeans any whitespace (such as a space character).

where\s表示任何空格(例如空格字符)。

回答by Dhanendra Pratap Singh

You just need this function. replaceAll()

你只需要这个功能。 replaceAll()

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

\s= Anything that is a space character (including space, tab characters etc)

\s= 任何空格字符(包括空格、制表符等)

You need to escape the backslash if you want \s to reach the regex engine, resulting in \s. Like wise we use :-

如果您希望 \s 到达正则表达式引擎,则需要转义反斜杠,从而导致 \s。像我们一样明智地使用:-

\S= Anything that isn't a space character (including both letters and numbers, as well as punctuation etc)

\S= 任何非空格字符(包括字母和数字,以及标点符号等)

\w= Anything that is a word character

\w= 任何是单词字符的东西

\W= Anything that isn't a word character (including punctuation etc)

\W= 任何非单词字符(包括标点符号等)