How to trim a string

Discussion in 'Ruby' started by dany@wu.net.nz, Oct 30, 2007.

  1. Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
    , Oct 30, 2007
    #1
    1. Advertising

  2. Tim Hunter Guest

    Dany Wu wrote:
    > 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.


    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"

    --
    RMagick OS X Installer [http://rubyforge.org/projects/rmagick/]
    RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618]
    RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]
    Tim Hunter, Oct 30, 2007
    #2
    1. Advertising

  3. 7stud -- Guest

    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/.
    7stud --, Oct 30, 2007
    #3
  4. Dany Wu Guest

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

    >

    ...<snipped> Some excellent examples </snipped>
    >
    > #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.
    --
    Posted via http://www.ruby-forum.com/.
    Dany Wu, Oct 30, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FAQ server
    Replies:
    0
    Views:
    138
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    135
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    213
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    294
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    130
    -Lost
    Apr 24, 2007
Loading...

Share This Page