在groovy中,如何分配多行字符串而不使用转义斜杠(\)并且不进行插值
在 groovy 中,如果我想要没有插值且没有转义的多行字符串,该怎么办
:
var1="hello hello"
var2="""/
adakldjkadj\^mk
as@da\kl#DFD#$#
${var1}
d3&657\7fdsfsf
/"""
println var2;
应该打印出完全相同的内容,例如:
adakldjkadj\^mk
as@da\kl#DFD#$#
${var1}
d3&657\7fdsfsf
也就是说,${var1} 尚未扩展,并且不需要转义 \,它是多行字符串
那么如何在 Groovy 中分配这个 Heredoc 字符串。这在 bash 脚本、ruby、perl 等中是可能的。
在 ruby 中,它表示为(注意分隔符周围的引号,如:'EOL')
a = <<'EOL'
adakldjkadj\^mk
as@da\kl#DFD#$#
yes ${var1}
d3&657\7fdsfsf
EOL
如何在 groovy 中做到这一点?
in groovy , what to do if i want multiline string without interpolation and WITHOUT ESCAPING
something like:
var1="hello hello"
var2="""/
adakldjkadj\^mk
as@da\kl#DFD#$#
${var1}
d3&657\7fdsfsf
/"""
println var2;
should print exactly the same as it is, like:
adakldjkadj\^mk
as@da\kl#DFD#$#
${var1}
d3&657\7fdsfsf
THAT IS, THE ${var1} has NOT been expanded, AND the escaping the \ was not needed and it is multiline string
THEN HOW TO ASSIGN THIS HEREDOC STRING IN GROOVY. This is possible in bash script, ruby,perl etc.
in ruby it is expressed as (notice the quotes around the delimiter chars like: 'EOL')
a = <<'EOL'
adakldjkadj\^mk
as@da\kl#DFD#$#
yes ${var1}
d3&657\7fdsfsf
EOL
how to do it in groovy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用三重单引号(如
'''
)代替双引号,以避免多行字符串中的变量插值。Use triple single quotes like
'''
instead of double quotes to avoid variable interpolation in multi-line strings.这是不可能的,请参见此处: https://issues.apache.org/jira/浏览/GROOVY-411
It is not possible, see here: https://issues.apache.org/jira/browse/GROOVY-411
使用单引号,您可以更接近,但仍然不是您想要的。它不会再扩展 ${var1} 。
至于 \,它始终是特殊字符的 java/groovy 分隔符,因此您始终必须对其进行转义。
编辑:看起来他们正在为 1.8 开发这个,或者已经在 1.8 中了。我目前只运行 1.7,因此无法测试或提供代码示例。
You can get closer, but still not what you're looking for, using single-quotes. It won't expand the ${var1} anymore.
As far as the \, that's always a java/groovy delimiter for special characters, so you'll always have to escape it.
Edit: Looks like they're working on this for 1.8, or it's already in 1.8. I'm currently only running 1.7, so can't test or provide a code example.