开发者

How to remove strings from all elements in array?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜