# String search backwards?

**URL:** <https://www.ruby-forum.com/t/string-search-backwards/96797>\
**Category:** Ruby\
**Created:** [January 24, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/string-search-backwards/96797 "2006-01-24T21:05:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 24, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/string-search-backwards/96797/1 "2006-01-24T21:05:41Z")

</div>

Hi all,

Can Ruby search a string backwards? I can’t find any reference to this  
in the docs or online, but it seems as if you should be able to do this.

What I would like, ideally, is a method like:

n = rstrstr( orig\_str, sub\_str)

where n is the starting index of the last occurrence of sub\_str in  
orig\_str. Anybody know how to do this? I tried using the ‘r’ modifier to  
// but that failed as well.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 24, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/string-search-backwards/96797/2 "2006-01-24T21:22:49Z")

</div>

n = orig\_str.rindex(sub\_str)
