【问题标题】:ruby on rails replace single-quotes with double-quote in a stringruby on rails 用双引号替换字符串中的单引号
【发布时间】:2021-12-22 09:53:31
【问题描述】:

我正在尝试用字符串中的双引号替换单引号,如下所示:

current_res = 25
lowest_res = 15
str = "The result of the child is '#{current_res}' and the lowest grade is '#{lowest_res }'."

我需要输出看起来像:

str = The result of the child is "25" and the lowest grade is "15".

我尝试了使用 gsub 的不同方法,但到目前为止没有任何效果。有什么想法吗?

【问题讨论】:

  • 我刚跑了puts str.gsub(/'/, '"')你想分享你使用的gsub调用吗?你能澄清你想要的输出吗?那应该是红宝石还是文字字符串输出? (如果是前者,那不是有效的 ruby​​ 代码;如果是后者,你打算运行什么命令以在开头给你str =

标签: ruby string escaping double-quotes single-quotes


【解决方案1】:

如果这是您所涵盖的唯一情况,那么您需要在双引号字符串中显示一些输出。像下面这样简单的东西怎么样

str = "The result of the child is \"#{current_res}\" and the lowest grade is \"#{lowest_res }\" ."

您可以在双引号字符串中转义引号。

【讨论】:

  • 或者str = %Q(The result of the child is "#{current_res}" and the lowest grade is "#{lowest_res }" .),如果你想避免所有的反斜杠。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 2011-12-07
  • 1970-01-01
  • 2011-07-08
  • 2023-03-23
相关资源
最近更新 更多