简体   繁体   中英

Ruby get last * in a string

I have a string with different data and stars as well. How can I get the index of the last *? For example

hello * there * *

Now how can I get the index of the 3rd *?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM