Ruby从数组中选择字符串(Ruby select string from array)

编程入门 行业动态 更新时间:2024-10-27 05:24:14
Ruby从数组中选择字符串(Ruby select string from array)

我基本上试图选择匹配特定字符串的数组中的每个字符串。

我目前正在使用以下代码,但正如大家所知,这只提供了一系列评估为true的元素的索引。 我想要那个位置的实际字符串。

arr.each_index.select{|i| arr[i].chars.sort.join == someString}

I am basically trying to select every string in an array that matches a particular string.

I am currently using the following code, but as you guys know, this only gives an array of the indexes of the elements that evaluate to true. I want the actual string at that position.

arr.each_index.select{|i| arr[i].chars.sort.join == someString}

最满意答案

尝试这个:

arr.select { |s| s == "some string" }

一个例子:

arr = %w(One Two Three Two Two) arr.select { |x| x == "Two" } => ["Two", "Two", "Two"]

Try this:

arr.select { |s| s == "some string" }

An example:

arr = %w(One Two Three Two Two) arr.select { |x| x == "Two" } => ["Two", "Two", "Two"]

更多推荐

本文发布于:2023-08-01 04:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1354040.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   组中   Ruby   array   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!