简体   繁体   English

GSUB! 干净的弦,带撇号

[英]gsub! clean string, taking away apostrophe

Trying to remove the apostrophe within a string. 尝试删除字符串中的撇号。 Using the code below I've removed the apostrophe and the spaces, can anyone help me fix it so I'm just removing the apostrophe? 使用下面的代码,我已经删除了撇号和空格,任何人都可以帮助我修复它,所以我只是删除了撇号?

def clean_uniname(text)
 return text.gsub!(/\s|"|'/, '')
end

This should do the trick: 这应该可以解决问题:

    def clean_uniname(text)
     text.gsub!("'", '')
    end

\\s means "whitespace character", and " represents a quote. If you don't want those in your regular expression, pull them. For debugging help try Rubular for testing. \\s表示“空格字符”,而"表示引号。如果您不希望在正则表达式中使用这些字符,请将其拉​​出。有关调试帮助,请尝试Rubular进行测试。

This means your code should be: 这意味着您的代码应为:

def clean_uniname(text)
  text.gsub!(/\s|"|'/, '')

  text
end

It's worth noting that gsub! 值得注意的是gsub! returns nil if no changes have been made so an explicit return of text is required here if you're using the return value. 如果未进行任何更改,则返回nil ,因此,如果您使用的是返回值,则此处需要显式返回text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM