0

我也有一个带有不同数据和星星的字符串。如何获取最后一个 * 的索引?例如

hello * there * *

现在我怎样才能得到第三个*的索引?

4

1 回答 1

4

Use String.rindex:

Returns the index of the last occurrence of the given substring or pattern (regexp) in str. Returns nil if not found.

x = 'hello * there * *'
puts x.rindex('*') # 16
于 2013-05-18T16:06:11.360 回答