[英]How to replace a String which is having escapes in Groovy?
I am unable to replace the string \"@salePriceAdjId@\" which is in a file in groovy.我无法替换 groovy 文件中的字符串 \"@salePriceAdjId@\"。 I have googled it but found no luck.我用谷歌搜索了它,但没有找到运气。
String str = new File("C:\\example.txt").getText();
Str.replaceAll("\\"+'"'@salePriceAdjId@\\"+'"',1234567890)
You should use Groovy's regexp literals for this.为此,您应该使用 Groovy 的正则表达式文字。 Also replaceAll()
doesn't mutate the str
, you have to reassign:另外replaceAll()
不会改变str
,您必须重新分配:
String str = 'a vvvvvvvvvvvv \"@salePriceAdjId@\" aaaaa' // new File("C:\\example.txt").text
String val = '\\"@salePriceAdjId@\\"' // get from somewhere
str = str.replaceAll( /$val/, '1234567890')
assert str == 'a vvvvvvvvvvvv 1234567890 aaaaa'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.