[英]String literal without need to escape backslash
在C#中,我可以写反斜杠和其他特殊字符,而不必在字符串前使用@
进行转义,但是我必须转义双引号。
C#
string foo = "The integer division operator in VisualBASIC is written \"a \\ b\"";
string bar = @"The integer division operator in VisualBASIC is written \"a \ b\""
在Ruby中,我可以使用单引号字符串文字,但是我想将其与"text #{value}"
类的字符串插值结合使用。 Ruby中的等效于C#中的@
吗?
Ruby中有一些类似的东西。 例如
foo = %Q(The integer division operator in VisualBASIC is written "a \\ b" and #{'interpolation' + ' works'})
您也可以在其中插入字符串。 唯一的警告是,您仍然需要转义\\
字符。
高温超导
您可以将heredoc与单引号一起使用。
foo = <<'_'
The integer division operator in VisualBASIC is written "a \ b";
_
如果要在末尾消除换行符,请chomp
它。
请注意,这不适用于字符串插值。 如果要在字符串中插入求值表达式,则可以在创建字符串后使用%
操作。
foo = <<'_'
text %{value}
_
foo.chomp % {value: "foo"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.