如何删除“字符串引用?

时间:2010-09-29 21:32:48

标签: ruby string

  

可能重复:
  ruby code for modifying outer quotes on strings?

这是我的字符串:

msgid """We couldn't set up that account, sorry.  Please try again, or contact an ""admin (link is above)."

我想删除除第一个和最后一个之外的所有双引号。 我该怎么办?

4 个答案:

答案 0 :(得分:1)

只要您知道在开头和结尾总是需要引用,这是一个选项。

假设x持有你想要操纵的刺痛。

x = '"' + x.gsub('"', '') + '"'

答案 1 :(得分:1)

这假设您的字符串将始终为上面显示的“msgid ...”格式,并且您的意图输出为“msgid”文本“':

>> str.gsub(/(msgid )"{1,}(.*) "{1,}(.*)"/, '\1"\2 \3"')
=> "msgid "We couldn't set up that account, sorry.  Please try again or contact an admin (link is above).""
>> puts str.gsub(/(msgid )"{1,}(.*) "{1,}(.*)"/, '\1"\2 \3"')
msgid "We couldn't set up that account, sorry.  Please try again or contact an admin (link is above)."

答案 2 :(得分:0)

我会选择这样的事情:

msg = %q{msgid """We couldn't set up that account, sorry.  Please try again, or contact an ""admin (link is above)."}
print 'msgid "' << msg.sub('msgid ', '').gsub('"', '') << '"'
# >> msgid "We couldn't set up that account, sorry.  Please try again, or contact an admin (link is above)."

如果“msgid”在每一行之前,则删除它,通过剥离它们来处理双引号,然后放回“msgid”并再次添加所需的前导和尾随双引号。

答案 3 :(得分:0)

msgid实际上是3个字符串,因为双引号的字符串分隔符。所以你可以:

msgid = ""<<"We couldn't set up that account, sorry. Please try again, or contact an "<<"admin (link is above)."

或者如果你想走很长的路:

x = %q{}
  msgid = """We couldn't set up that account, sorry. Please try again, or contact an ""admin (link is above)."
  x << msgid
  x.gsub!('"', '')
  msgid = ""
  msgid << x
# Check
  puts x