substring: to the end of the string

Discussion in 'Ruby' started by KONTRA Gergely, Nov 14, 2003.

  1. Hi!

    Is there any easiest way to get the substring starting from the 3rd
    character?

    my solution:
    str[3..-1] which seems quite odd :-/

    What if, intuitive, we can write
    str[3..]?

    Any smarter solution?

    Gergo
    --
    +-[ Kontra, Gergely<> PhD student Room IB113 ]---------+
    | http://www.mcl.hu/~kgergely "Olyan langesz vagyok, hogy |
    | Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
    +-- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+
    KONTRA Gergely, Nov 14, 2003
    #1
    1. Advertising

  2. "KONTRA Gergely" <> schrieb im Newsbeitrag
    news:...
    > Hi!
    >
    > Is there any easiest way to get the substring starting from the 3rd
    > character?
    >
    > my solution:
    > str[3..-1] which seems quite odd :-/
    >
    > What if, intuitive, we can write
    > str[3..]?
    >
    > Any smarter solution?


    Not as far as I know. You can do str[3,str.length-3] but this isn't
    really an improvement unless you value the absence of negative integers in
    this expression. :)

    robert
    Robert Klemme, Nov 14, 2003
    #2
    1. Advertising

  3. > my solution:
    > str[3..-1] which seems quite odd :-/


    The idiom in python is str[3:]
    Conversely, str[:3] gives you the 3 1st chars.

    Read the explanation of slicing operations here:
    http://www.python.org/doc/current/lib/typesseq.html
    (read note 4 & 5)

    And beware, 'str' is a builtin, hence a poor choice as a variable name.

    Have fun,

    Bernard.
    Bernard Delmée, Nov 15, 2003
    #3
  4. Oops - wrong NG, my bad ;-)
    Bernard Delmée, Nov 15, 2003
    #4
  5. On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote:

    > Hi!


    > Is there any easiest way to get the substring starting from the 3rd
    > character?


    > my solution:
    > str[3..-1] which seems quite odd :-/


    Seems quite even to me :)

    Seriously, the ability to negative-index strings and arrays like that
    is a breath of fresh air compared to traditional compiled languages.
    I see no need to improve on it.

    > What if, intuitive, we can write
    > str[3..]?


    I rather doubt that's going to happen. When you execute str[3..7],
    the 3..7 isn'y arbitrary syntax, it's a Range object. If Range
    objects were allowed to be unbounded, then fine. So the question
    really becomes, should Range objects allow unbounded ranges?

    > Any smarter solution?


    Write a String#substring method if you like.

    Cheers,
    Gavin
    Gavin Sinclair, Nov 15, 2003
    #5
  6. class String

    def substring

    /(^...)(.*)\z/ =~ self

    return $2

    end

    end

    "123456".substring
    = >"456"


    Now......

    I am just wondering how can I build dinamically the Regexp
    I mean ...

    "123456".substring(5)
    = > "6"

    "123456".substring(4)
    = > "56"

    ideas ?

    -r.



    KONTRA Gergely wrote:

    >Hi!
    >
    >Is there any easiest way to get the substring starting from the 3rd
    >character?
    >
    >my solution:
    >str[3..-1] which seems quite odd :-/
    >
    >What if, intuitive, we can write
    >str[3..]?
    >
    >Any smarter solution?
    >
    >Gergo
    >
    >


    --
    General Electric - CIAT
    Advanced Engineering Center
    ________________________________________
    Rodrigo Bermejo
    Information Technologies.
    Special Applications
    Dial-comm : *879-0644
    Phone :(+52) 442-196-0644
    Bermejo, Rodrigo, Nov 15, 2003
    #6
  7. KONTRA Gergely

    Dan Doel Guest

    Some ways:

    class String
    def substring1(n)
    reg = Regexp.compile("(^" + "."*n + ")(.*)\\z")
    reg.match(self)[2]
    end

    def substring2(s, e=-1)
    self[s..e]
    end
    end

    p "12345".substring1 1
    p "12345".substring1 2
    p "12345".substring2 1
    p "12345".substring2 2
    p "12345".substring2 1, 3
    Dan Doel, Nov 15, 2003
    #7
  8. On Saturday, November 15, 2003, 12:24:42 PM, Rodrigo wrote:

    > class String
    > def substring
    > /(^...)(.*)\z/ =~ self
    > return $2
    > end
    > end


    > "123456".substring
    > = >"456"


    > Now......


    > I am just wondering how can I build dinamically the Regexp
    > I mean ...


    > "123456".substring(5)
    > = > "6"


    > "123456".substring(4)

    = >> "56"

    > ideas ?


    Regexen are overkill here.

    class String
    def substring(m, n=-1)
    self[m..n]
    end
    end


    Gavin
    Gavin Sinclair, Nov 15, 2003
    #8
  9. Gavin Sinclair wrote:
    > On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote:
    > Seriously, the ability to negative-index strings and arrays like that
    > is a breath of fresh air compared to traditional compiled languages.
    > I see no need to improve on it.


    I do, but I have no good solution to the problem.

    The problem being that positive indexes go from 0 to n-1, while negative
    go from -1 to -n, which could introduce confusion.

    But as I said, I know of no good solutions, since -0 is not a good way,
    and making the first positive index a 1 is just as bad, for several reasons.
    Linus Sellberg, Nov 15, 2003
    #9
  10. "KONTRA Gergely" <> wrote in message news:...
    > Hi!
    >
    > Is there any easiest way to get the substring starting from the 3rd
    > character?
    >
    > my solution:
    > str[3..-1] which seems quite odd :-/
    >
    > What if, intuitive, we can write
    > str[3..]?
    >
    > Any smarter solution?
    >


    Actually I find this behavior annoying:

    irb(main):015:0> "hello"[3..-1]
    => "lo"
    irb(main):016:0> "hello"[4..-1]
    => "o"
    irb(main):017:0> "hello"[5..-1]
    => ""
    irb(main):018:0> "hello"[6..-1]
    => nil

    --
    J. Lambert
    Jon A. Lambert, Nov 15, 2003
    #10
  11. Hi!

    * KONTRA Gergely; 2003-11-14, 23:30 UTC:
    > Is there any easiest way to get the substring starting from the 3rd
    > character?
    >
    > my solution:
    > str[3..-1] which seems quite odd :-/
    >
    > What if, intuitive, we can write
    > str[3..]?
    >
    > Any smarter solution?


    Besids 'get used to -1'? Well, here's some extension to 'String'. I
    did try my best to follow POLS. The intention is that left, right,
    mid emulate BASIC's left$, right$ and mid$ while head and tail
    without any arguments have the results they should have from a
    functional programming point of view.

    class String

    # 'number' leftmost chars
    def left(number = 1)
    self[0..number-1]
    end

    # 'number' rightmost chars
    def right(number = 1)
    self[-number..-1]
    end

    # 'number' chars starting at position 'from'
    def mid(from, number=1)
    self[from..from+number]
    end

    # chars from beginning to 'position'
    def head(position = 0)
    self[0..position]
    end

    # chars following 'position'
    def tail(position = 0)
    self[position+1..-1]
    end

    end


    Josef 'Jupp' Schugt
    --
    .-------.
    message > 100 kB? / | |
    sender = spammer? / | R.I.P.|
    text = spam? / ___| |___
    Josef 'Jupp' SCHUGT, Nov 15, 2003
    #11
  12. On Sun, 16 Nov 2003 06:04:51 +0900, Josef 'Jupp' SCHUGT wrote:

    > Besids 'get used to -1'? Well, here's some extension to 'String'. I
    > did try my best to follow POLS. The intention is that left, right,
    > mid emulate BASIC's left$, right$ and mid$ while head and tail
    > without any arguments have the results they should have from a
    > functional programming point of view.
    >

    [snip class String extension]

    Useful.. I have added your snippet at rubygarden (StringSub):
    http://www.rubygarden.org/ruby?StandardClassExtensions/String


    --
    Simon Strandgaard
    Simon Strandgaard, Nov 15, 2003
    #12
  13. On Sun, 16 Nov 2003 06:04:51 +0900, Josef 'Jupp' SCHUGT wrote:

    > class String
    > def left(number = 1)
    > self[0..number-1]
    > end
    > def right(number = 1)
    > self[-number..-1]
    > end
    > def mid(from, number=1)
    > self[from..from+number]
    > end
    > def head(position = 0)
    > self[0..position]
    > end
    > def tail(position = 0)
    > self[position+1..-1]
    > end
    > end




    Maybe some destructive versions of the above methods could be useful?

    input="diamonds are forever"
    p input.right!(4) #-> "ever"
    p input.left!(4) #-> "diam"
    p input # -> "onds are for"

    --
    Simon Strandgaard
    Simon Strandgaard, Nov 16, 2003
    #13
  14. KONTRA Gergely

    Guest

    Hi,

    At Sat, 15 Nov 2003 23:58:55 +0900,
    Austin Ziegler wrote:
    > As Gavin pointed out, regexen are overkill here. However, the
    > technique is worth noting:


    Another syntax sugar,

    > class String
    > def substring(start, len = nil)
    > if len.nil?

    self[/\A.{#{start}}(.*)/m, 1]
    > else

    self[/\A.{#{start}}(.{#{len}})/m, 1]
    > end
    > $1
    > end
    > end


    --
    Nobu Nakada
    , Nov 17, 2003
    #14
  15. [OT] Re: substring: to the end of the string

    "Linus Sellberg" <> schrieb im Newsbeitrag
    news:bp53n1$4o7$...
    > Gavin Sinclair wrote:
    > > On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote:
    > > Seriously, the ability to negative-index strings and arrays like that
    > > is a breath of fresh air compared to traditional compiled languages.
    > > I see no need to improve on it.

    >
    > I do, but I have no good solution to the problem.
    >
    > The problem being that positive indexes go from 0 to n-1, while negative
    > go from -1 to -n, which could introduce confusion.
    >
    > But as I said, I know of no good solutions, since -0 is not a good way,


    This reminds me of an old mathematician's joke: there was this professor
    for math that found an epsilon so small, that half of it was already
    negative... ;-)

    > and making the first positive index a 1 is just as bad, for several

    reasons.

    Yup

    robert
    Robert Klemme, Nov 17, 2003
    #15
  16. On 1115, Gavin Sinclair wrote:
    > I rather doubt that's going to happen. When you execute str[3..7],
    > the 3..7 isn'y arbitrary syntax, it's a Range object. If Range
    > objects were allowed to be unbounded, then fine. So the question
    > really becomes, should Range objects allow unbounded ranges?


    Yes, it should :) What about 3..INF here?

    --
    +-[ Kontra, Gergely<> PhD student Room IB113 ]---------+
    | http://www.mcl.hu/~kgergely "Olyan langesz vagyok, hogy |
    | Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
    +-- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+
    KONTRA Gergely, Nov 18, 2003
    #16
  17. --On Saturday, November 15, 2003 12:48 AM +0900 KONTRA Gergely
    <> wrote:

    > Hi!
    >
    > Is there any easiest way to get the substring starting from the 3rd
    > character?
    >
    > my solution:
    > str[3..-1] which seems quite odd :-/


    I typically use str[3..str.length].. It's probably slower, but it looks a
    bit cleaner in code..

    Regards,
    -JD-
    Jason DiCioccio, Nov 18, 2003
    #17
    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. juli
    Replies:
    3
    Views:
    8,850
    Shiva
    Dec 6, 2004
  2. Dave
    Replies:
    3
    Views:
    357
    Christopher Boomer
    Aug 6, 2003
  3. Harvey Thomas
    Replies:
    0
    Views:
    370
    Harvey Thomas
    Aug 6, 2003
  4. Replies:
    3
    Views:
    191
    Sherm Pendley
    Aug 3, 2005
  5. SM
    Replies:
    4
    Views:
    205
Loading...

Share This Page