简体   繁体   English

如何将数组传递给ruby中的sub或gsub?

[英]How to pass array to sub or gsub in ruby?

I have an array of characters I want to be removed from a string: 我有一个字符数组,我想从字符串中删除:

stops = ["[", "]", "^", "(", ")", "#", "*", "?", "~"]

I want to be able to pass the array and have all occurrences of those chars removed so that: 我希望能够传递数组并删除所有出现的chars,以便:

"str [with] unwanted# char*acters"

becomes

"str with unwanted characters"

"str [with] unwanted# char*acters".gsub(Regexp.union(stops), '')
# => "str with unwanted characters"

如果您需要删除字符,可以使用#delete

str.delete "[]^()#*?~"
str.tr('[]^()#*?~','')
str.tr('[]^()#*?~','abcdefghi')

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

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