From: Robert Klemme Date: 2008-08-20T01:11:38+09:00 Subject: Re: Cut a string if length > n On 19.08.2008 17:45, ara.t.howard wrote: > On Aug 19, 2008, at 9:25 AM, Pål Bergström wrote: > >> What's the best way to cut a string if the length is above n >> characters? >> Is it slice, or is there any other convenient method? Trying to >> understand the string class from the docs but not sure which one to >> use. > > > this_always_works = string[ 0 .. n ] > > regardless of whether the string is <, =, or > than n I believe slice! also always works: irb(main):011:0> s="abcdefghijklmnop" => "abcdefghijklmnop" irb(main):012:0> s.length => 16 irb(main):013:0> s.slice! 50..-1 => nil irb(main):014:0> s => "abcdefghijklmnop" irb(main):015:0> s.slice! 5..-1 => "fghijklmnop" irb(main):016:0> s => "abcde" It looks uglier but it modifies the string in place which might come in handy at times. Kind regards robert