开发者

How to remove strings from all elements in array?

开发者 https://www.devze.com 2022-12-12 04:10 出处:网络
How can i remove a repeating string keywo开发者_运维百科rd from all elements in an array ?I think you mean you have an array of strings and they all contain some substring that you want to remove. Non

How can i remove a repeating string keywo开发者_运维百科rd from all elements in an array ?


I think you mean you have an array of strings and they all contain some substring that you want to remove. Non-destructively:

array.map {|s| s.gsub(keyword, '')}

Use destructive variants as desired to do it in-place.


Are you referring to string in the array, or non-unique elements. For the first, use the uniq method:

p ["foo", "bar", "foo", "baz"].uniq
["foo", "bar", "baz"]

For the latter, try something like:

p ["foo", "bar", "foo", "baz"].map { |x| x.gsub('oo', '') }
["f", "bar", "f", "baz"]
0

精彩评论

暂无评论...
验证码 换一张
取 消