From: Robert Klemme Date: 2009-10-29T05:55:06+09:00 Subject: Re: String Matching Problem On 10/28/2009 09:22 PM, Matt Brooks wrote: > I need a way to accomplish the following. I can't figure out an elegant > way, beating my head against the wall thinking about it. > > I receive strings into a variable and want to check that string to see > if it contains at least the beginning of another string. > > > say original string is "CHLE,231,1", and i receive string I want to > check against it. > > I guess you could say the strings are comma separated within each string > that I receive. > > I need to know it matched if I receive "CHLE,231" or "CHLE,231,1" but if > I get "CHLE,23" or "CHLE,231,2" these should not be matches, and should > say nil. > > > > For Example > > blah = "CHLE,231,1" > => "CHLE,231,1" > > blah["CHLE,231,1"] > => "CHLE,231,1" * GOOD > > blah["CHLE,231"] > => "CHLE,231" * GOOD > > blah["CHLE,232"} > => nil * GOOD > > blah["CHLE,231,2"] > => nil * GOOD > > blah["CHLE,23"] > => "CHLE,23" * BAD, I want this to be NIL > > > Thanks for the help!!! You can split your original string into an Array and start testing from the first element on. irb(main):001:0> check = 'CHLE,231,1' => "CHLE,231,1" irb(main):002:0> prepared = check.split /,+/ => ["CHLE", "231", "1"] Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/