On Tue, 2007-11-06 at 21:30 +0900, Jari Williamsson wrote:
> 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 Williamsson

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