我正在使用 Ruby 2.4。如何找到出现在另一个字符串中特定索引之前的字符串的第一个索引?也就是说,假设我有这个字符串
"ab cd ab cd ab cd ef"
我想找到上面字符串中索引 10 之前出现的“ab”的第一个索引。在这个例子中,答案是索引 6。如果我想在上面的字符串中找到索引 5 之前出现的“ab”的索引,答案将是零。我不确定如何使用 Ruby 的“索引”函数来实现我想要的。
str = "ab cd ab cd ab cd ef"
# find the first index of "ab", working backwards from index 10:
p str.rindex("ab", 10) # => 6
rindex
我猜是“reverse_index”的缩写。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句