gpt4 book ai didi

Ruby 从数组中选择字符串

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:38 25 4
gpt4 key购买 nike

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

我目前正在使用以下代码,但正如你们所知,这只给出了一个评估为 true 的元素的索引数组。我想要那个位置的实际字符串。

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"]

关于Ruby 从数组中选择字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21448868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com