You can find the last index of a character in a string in Ruby by using the rindex
method. The rindex
method takes a character as an argument and returns the index of the last occurrence of that character in the string.
Here's an example:
main.rb77 chars4 lines
In the example above, we use the rindex
method to find the last index of the character "o" in the string "hello world". The last_index
variable will hold the value 7, which is the index of the last occurrence of "o" in the string.
Note that the rindex
method is case-sensitive, so if you want to search for a character regardless of case, you can convert the string and the search character to a common case before using the rindex
method.
gistlibby LogSnag