# Start of string?

**URL:** <https://www.ruby-forum.com/t/start-of-string/119368>\
**Category:** Ruby\
**Created:** [November 6, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/start-of-string/119368 "2007-11-06T13:59:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 6, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/start-of-string/119368/1 "2007-11-06T13:59:21Z")

</div>

What’t the most elegant way to find a substring match at the very start  
of a string?

Currently I’m using this approach:  
a = “long string”  
key = “long”  
if a[0, key.length] == key then

…while this might look ok, it doesn’t look so good with constant  
strings:  
if a[0, 4] == “long” then  
or  
if a[0, “long”.length] == “long” then

I guess what I’m looking for is something like:  
if a.startswith(“long”) then

Is there any such solution?

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 6, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/start-of-string/119368/2 "2007-11-06T14:17:32Z")

</div>

Hi –

On Tue, 6 Nov 2007, Jari W. wrote:

> or  
> if a[0, “long”.length] == “long” then
> 
> I guess what I’m looking for is something like:  
> if a.startswith(“long”) then
> 
> Is there any such solution?

You could do:

if a.index(“long”) == 0

David

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 6, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/start-of-string/119368/3 "2007-11-06T14:20:27Z")

</div>

Alle martedì 6 novembre 2007, Jari W. ha scritto:

> or  
> Jari W.  
> You can use a regexp:

if a.match /^long/ then  
…

The ^ at the beginning of the regexp means that the regexp should be  
matched  
at the beginning of the string (actually, of the line. If your string is  
multiline, you need to use \A to match the beginning of the string).

If the string you want to look for is stored in a variable, you can use  
string  
interpolation inside the regexp:

str = “long”  
if a.match /^#{str}/ then

Of course, this can cause problems if str contains characters which are  
special in a regexp. In this case, you need to quote it:

str = “long”  
if a.match /^#{Regexp.quote(str)}/ then

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 6, 2007, 2:21pm UTC](https://www.ruby-forum.com/t/start-of-string/119368/4 "2007-11-06T14:21:37Z")

</div>

Hi!

Take a look at Regexp class:

[class Regexp - RDoc Documentation](http://www.ruby-doc.org/core/classes/Regexp.html)

…after that your starts\_with? method should be similar to this one:

class String  
def starts\_with?(a\_string)  
self =~ Regexp.new(‘^%s’ % [a\_string])  
end  
end

a = “long string”  
a.starts\_with?(“long”) # =\> 0  
a.starts\_with?(“not soo long”) # =\> nil

!note: in ruby 0 (Fixnum) means true in a condition, so does “”  
(String), but nil (NilClass) means false.

Cheers, Bence

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 6, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/start-of-string/119368/5 "2007-11-06T14:22:47Z")

</div>

Jari,

> Is there any such solution?

Here is a regular expression one:

“longstring” =~ /^long/

HTH,  
Peter

* * *

[http://www.rubyrailways.com](http://www.rubyrailways.com)  
[http://scrubyt.org](http://scrubyt.org)

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 6, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/start-of-string/119368/6 "2007-11-06T17:14:53Z")

</div>

Hi,

At Tue, 6 Nov 2007 22:02:27 +0900,  
David A. Black wrote in [ruby-talk:277691]:

> You could do:
> 
> if a.index(“long”) == 0

rindex(“long”, 0) is faster for long but unmatching strings.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 6, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/start-of-string/119368/7 "2007-11-06T14:26:18Z")

</div>

On Tue, 2007-11-06 at 21:30 +0900, Jari W. wrote:

> or  
> Jari W.  
> How about;

if /^long/.match a

if a.match /^long/

if a.index(‘long’).zero?

The last one probably has the best portability for your non-constant  
keys.

Arlen
