"Bill Kelly" <billk / cts.com> schrieb im Newsbeitrag 
news:020b01c559ef$fc194ee0$6442a8c0 / musicbox...
> Hi,
>
> Is there any ruby built-in that might tell me how
> many characters match (are in common) from the start
> (left) of two strings?  E.g.
>
> "abcxxxxxx".lcommon("abcdezzzzz")  # => "abc" (or 3)
>
> All I need is the length but a substring would be
> fine too.
>
> Figured I'd ask in case I'd overlooked a nicer way
> to do this than writing a loop.  :)

Regexps can help here - dunno about performance

class String
  def lcommon(s)
    len = s.length
    Regexp.new("^" << s.gsub( /./, '(?:\\&' ) << ( ")?" * 
len ) ).match(self)[0]
  end
end

>> s1 = "abcxxxxxx"
=> "abcxxxxxx"
>> s2 = "abcdezzzzz"
=> "abcdezzzzz"
>> s1.lcommon s2
=> "abc"

:-)

Kind regards

    robert