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"]更多推荐
发布评论