简体   繁体   中英

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:

"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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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