首页 > 解决方案 > ruby on rails 用双引号替换字符串中的单引号

问题描述

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

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 的不同方法,但到目前为止没有任何效果。有任何想法吗?

标签: rubystringescapingdouble-quotessingle-quotes

解决方案


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

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

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


推荐阅读