From: 7stud -- Date: 2007-10-30T12:03:00+09:00 Subject: Re: How to trim a string Dany Wu wrote: > ...I'm looking for a way to return an x number of characters > from a string, eg. 5 characters starting from position 10 in the string, > the last 10 chars, the first 15 chars, etc. > > Is there a String method that does this? I can't seem to find any but I > may have missed something glaringly obvious. Any help would be greatly > appreciated. > str = "In the absence of justice, what is sovereignty but organized robbery?" #5 characters starting from position 10 in the string: sub_str = str[10, 5] #first char is pos 0, so pos 10 is 11th char puts sub_str + "<----" --output:-- ence <---- #the last 10 chars: sub_str = str[-10..-1] puts sub_str + "<----" --output:-- d robbery? #the first 15 chars: sub_str = str[0...15] #notice the 3 dots v. 2 dots puts sub_str + "<----" --output:-- In the absence <---- -- Posted via http://www.ruby-forum.com/.