在Ruby中的字符串中转义单引号和双引号? [英] Escaping single and double quotes in a string in ruby?

查看:281
本文介绍了在Ruby中的字符串中转义单引号和双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在字符串中转义单引号和双引号?

How can I escape single and double quotes in a string?

我想一起转义单引号和双引号。我知道如何分别通过它们,但不知道如何同时通过它们。

I want to escape single and double quotes together. I know how to pass them separately but don't know how to pass both of them.

例如: str = ruby​​'on rails = ruby​​'on rails

推荐答案

我的首选方式是不必担心转义使用%q ,其行为类似于单引号字符串(不进行插值或字符转义),或者使用%Q 双引号字符串行为:

My preferred way is to not worry about escaping and instead use %q, which behaves like a single-quote string (no interpolation or character escaping), or %Q for double quoted string behavior:

str = %q[ruby 'on rails" ] # like single-quoting
str2 = %Q[quoting with #{str}] # like double-quoting: will insert variable

请参见 https://docs.ruby-lang.org/en /trunk/syntax/literals_rdoc.html#label-Strings 并搜索%字符串

这篇关于在Ruby中的字符串中转义单引号和双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆