# String#starts\_with?

**URL:** <https://www.ruby-forum.com/t/string-starts-with/97620>\
**Category:** Ruby\
**Created:** [July 8, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/string-starts-with/97620 "2006-07-08T19:57:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 8, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/string-starts-with/97620/1 "2006-07-08T19:57:17Z")

</div>

Has anyone run tests to see what the fastest way to do a  
String#starts\_with?(otherstring) is? I’ll need this in an inner loop,  
so it’s worth squeezing out the few drops of extra speed.

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 8, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/string-starts-with/97620/2 "2006-07-08T21:14:30Z")

</div>

Martin DeMello wrote:

> Has anyone run tests to see what the fastest way to do a  
> String#starts\_with?(otherstring) is? I’ll need this in an inner loop,  
> so it’s worth squeezing out the few drops of extra speed.

String#index should be the fastest

lopex

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 8, 2006, 9:18pm UTC](https://www.ruby-forum.com/t/string-starts-with/97620/3 "2006-07-08T21:18:34Z")

</div>

On 7/9/06, Marcin Miel¿yñski [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Martin DeMello wrote:
> 
> > Has anyone run tests to see what the fastest way to do a  
> > String#starts\_with?(otherstring) is? I’ll need this in an inner loop,  
> > so it’s worth squeezing out the few drops of extra speed.
> 
> String#index should be the fastest

thanks. any suggestions for #ends\_with?

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 8, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/string-starts-with/97620/4 "2006-07-08T21:25:10Z")

</div>

Marcin MielÅ¼yÅ?ski wrote:

> String#index should be the fastest

I’m afraid it isn’t suitable for this case. String#index searches the  
whole string when the string doesn’t start with the start, which is very  
bad.

Use String#[] or a regular expression:

text = “this is a test”  
search = “this”  
text[0, search.length] == search #=\> true

text =~ /^this/ #=\> 0 (true)  
text =~ /^test/ #=\> nil (false)

Cheers,  
Robin S.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 8, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/string-starts-with/97620/5 "2006-07-08T21:32:14Z")

</div>

Martin DeMello wrote:

> thanks. any suggestions for #ends\_with?

Yes, two examples:

“this is a test” =~ /test$/ #=\> 10 (true)

text = “this is a test”  
search = “test”  
text[text.length - search.length, search.length] == search #=\> true
