How to trim a string

Hi everyone,

I’m a newbie who’s just started experimenting with Ruby and Rails. At
the moment 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.

Cheers,
Dany.

Dany Wu wrote:

Cheers,
Dany.

String#[] takes a variety of arguments that support the activities you
describe.

-------------------------------------------------------------- String#[]
str[fixnum] => fixnum or nil
str[fixnum, fixnum] => new_str or nil
str[range] => new_str or nil
str[regexp] => new_str or nil
str[regexp, fixnum] => new_str or nil
str[other_str] => new_str or nil
str.slice(fixnum) => fixnum or nil
str.slice(fixnum, fixnum) => new_str or nil
str.slice(range) => new_str or nil
str.slice(regexp) => new_str or nil
str.slice(regexp, fixnum) => new_str or nil
str.slice(other_str) => new_str or nil

  Element Reference---If passed a single Fixnum, returns the code of
  the character at that position. If passed two Fixnum objects,
  returns a substring starting at the offset given by the first, and
  a length given by the second. If given a range, a substring
  containing characters at offsets given by the range is returned.
  In all three cases, if an offset is negative, it is counted from
  the end of str. Returns nil if the initial offset falls outside
  the string, the length is negative, or the beginning of the range
  is greater than the end.

  If a Regexp is supplied, the matching portion of str is returned.
  If a numeric parameter follows the regular expression, that
  component of the MatchData is returned instead. If a String is
  given, that string is returned if it occurs in str. In both cases,
  nil is returned if there is no match.

     a = "hello there"
     a[1]                   #=> 101
     a[1,3]                 #=> "ell"
     a[1..3]                #=> "ell"

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 <----

7stud – wrote:

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.

… Some excellent examples

#the first 15 chars:

sub_str = str[0…15] #notice the 3 dots v. 2 dots
puts sub_str + “<----”

–output:–
In the absence <----

Thank you very much for the detailed explanation. Much appreciated.

Cheers,
Dany.