首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在groovy中将"<space>“替换为"\\<space>”

如何在groovy中将"<space>“替换为"\\<space>”
EN

Stack Overflow用户
提问于 2022-03-11 09:30:41
回答 2查看 61关注 0票数 0

试着

代码语言:javascript
复制
def resultString="Hello word result Hello: 0 Word: 0 void: 0"
def resultString1=resultString.replaceAll(' ', '\\\\ ')
println resultString1

预期结果:

代码语言:javascript
复制
Hello\\ word\\ result\\ Hello:\\ 0\\ Word:\\ 0\\ void:\\ 0

实际获得:

代码语言:javascript
复制
Hello\ word\ result\ Hello:\ 0\ Word:\ 0\ void:\ 0

任何输入都会非常有用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-11 09:54:35

def resultString="Hello word result Hello: 0 Word: 0 void: 0" println(resultString.replaceAll("\\s","\\\\\\\\"))

票数 1
EN

Stack Overflow用户

发布于 2022-03-11 09:43:05

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-

replaceAll使用Regex匹配,这会改变转义的行为。

使用replace代替:

代码语言:javascript
复制
def resultString="Hello word result Hello: 0 Word: 0 void: 0"
def resultString1=resultString.replace(' ','\\\\')
println resultString1

产出:

代码语言:javascript
复制
Hello\\word\\result\\Hello:\\0\\Word:\\0\\void:\\0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71436373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档