# How do I match strings?

**URL:** <https://www.ruby-forum.com/t/how-do-i-match-strings/163177>\
**Category:** Rails\
**Created:** [March 17, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/how-do-i-match-strings/163177 "2009-03-17T23:24:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 17, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/how-do-i-match-strings/163177/1 "2009-03-17T23:24:46Z")

</div>

Ok I know that sounds newbish, but I don’t want to match the entire  
string. I just want to match the first 25 characters or so.

I have a hash that looks like

somestuff[:something]

Now I want somestuff[:something] to equal a string.

somestuff[:something] == ‘sadfasdfsdfasdf’

the problem is that somestuff[:something] will never match the entire  
string, so I want to match the first 25 characters.

Any ideas…I hope that made sense?

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 17, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/how-do-i-match-strings/163177/2 "2009-03-17T23:42:06Z")

</div>

On Mar 17, 2009, at 3:24 PM, Chris G. wrote:

> somestuff[:something] == ‘sadfasdfsdfasdf’
> 
> the problem is that somestuff[:something] will never match the entire  
> string, so I want to match the first 25 characters.
> 
> Any ideas…I hope that made sense?

somestuff[:something][0,25] = “asdfasdfasdf”

Is one way. There’s probably a ruby method to match N characters of a  
string similar to C’s strncmp too.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 18, 2009, 12:34am UTC](https://www.ruby-forum.com/t/how-do-i-match-strings/163177/3 "2009-03-18T00:34:16Z")

</div>

Chris G. wrote:

> Ok I know that sounds newbish, but I don’t want to match the entire  
> string. I just want to match the first 25 characters or so.
> 
> I have a hash that looks like
> 
> somestuff[:something]
> 
> Now I want somestuff[:something] to equal a string.
> 
> somestuff[:something] == ‘sadfasdfsdfasdf’
> 
> the problem is that somestuff[:something] will never match the entire  
> string, so I want to match the first 25 characters.
> 
> Any ideas…I hope that made sense?

There are a number of ways depending on your needs. In your case if you  
just want to match the beginning of the string Rails provides the  
starts\_with method (Ruby 1.9 provides start\_with, but not Ruby 1.8). For  
more complex matches Ruby has excellent regex support. Look at the ~=  
method.

somestuff[:something].starts\_with “whatever”

somestuff[:something] ~= /^whatever.+/i

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 18, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/how-do-i-match-strings/163177/4 "2009-03-18T16:52:40Z")

</div>

Robert W. wrote:

> just want to match the beginning of the string Rails provides the  
> starts\_with method (Ruby 1.9 provides start\_with, but not Ruby 1.8). For  
> more complex matches Ruby has excellent regex support. Look at the ~=  
> method.
> 
> somestuff[:something].starts\_with “whatever”

Thanks for the info…

Can “whatever” be a variable?
