String slice assignment

Discussion in 'Javascript' started by Ron Garret, Apr 2, 2009.

  1. Ron Garret

    Ron Garret Guest

    Is there a way to do string slice assignment in Javascript other than
    tearing the string apart and reassembling it? I want to do the
    equivalent of:

    s1[x:y] = s2

    in Python. Is there a better way than (something like) this?

    s1 = s1.slice(0,x) + s2 + s1.slice(y)

    Thanks,
    rg
     
    Ron Garret, Apr 2, 2009
    #1
    1. Advertising

  2. Ron Garret <> writes:

    > Is there a way to do string slice assignment in Javascript other than
    > tearing the string apart and reassembling it?


    No. Strings in Javascript are immutable, so you can't assign to a
    slice of a string.

    > I want to do the
    > equivalent of:
    >
    > s1[x:y] = s2
    >
    > in Python. Is there a better way than (something like) this?
    >
    > s1 = s1.slice(0,x) + s2 + s1.slice(y)


    Not really, no.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, Apr 2, 2009
    #2
    1. Advertising

  3. Ron Garret

    Ron Garret Guest

    In article <>,
    Lasse Reichstein Nielsen <> wrote:

    > Ron Garret <> writes:
    >
    > > Is there a way to do string slice assignment in Javascript other than
    > > tearing the string apart and reassembling it?

    >
    > No. Strings in Javascript are immutable, so you can't assign to a
    > slice of a string.
    >
    > > I want to do the
    > > equivalent of:
    > >
    > > s1[x:y] = s2
    > >
    > > in Python. Is there a better way than (something like) this?
    > >
    > > s1 = s1.slice(0,x) + s2 + s1.slice(y)

    >
    > Not really, no.


    OK, just thought I'd ask. Thanks.

    rg
     
    Ron Garret, Apr 3, 2009
    #3
  4. Ron Garret

    Thomas Allen Guest

    On Apr 3, 2:06 am, Ron Garret <> wrote:
    > In article <>,
    >  Lasse Reichstein Nielsen <> wrote:
    >
    >
    >
    > > Ron Garret <> writes:

    >
    > > > Is there a way to do string slice assignment in Javascript other than
    > > > tearing the string apart and reassembling it?  

    >
    > > No. Strings in Javascript are immutable, so you can't assign to a
    > > slice of a string.

    >
    > > > I want to do the
    > > > equivalent of:

    >
    > > > s1[x:y] = s2

    >
    > > > in Python.  Is there a better way than (something like) this?

    >
    > > > s1 = s1.slice(0,x) + s2 + s1.slice(y)

    >
    > > Not really, no.

    >
    > OK, just thought I'd ask.  Thanks.


    This got me thinking about writing a little String.prototype method,
    but due to Strings' immutability, there's no way to change the value
    from within the method. This is a little better though if you find
    yourself doing that sort of substitution frequently.

    String.prototype.rangeReplace = function(x, y, str) {
    return this.slice(0, x) + str + this.slice(y);
    }
    newGreeting = 'Hello'.rangeReplace(1, 3, 'oh');

    Thomas
     
    Thomas Allen, Apr 3, 2009
    #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. Rich Krauter

    Assignment to slice

    Rich Krauter, Jan 21, 2004, in forum: Python
    Replies:
    0
    Views:
    362
    Rich Krauter
    Jan 21, 2004
  2. James Henderson

    Re: Assignment to slice

    James Henderson, Jan 21, 2004, in forum: Python
    Replies:
    2
    Views:
    298
    James Henderson
    Jan 22, 2004
  3. Rich Krauter

    RE: Assignment to slice

    Rich Krauter, Jan 21, 2004, in forum: Python
    Replies:
    6
    Views:
    352
    sambo
    Jan 23, 2004
  4. Jens Lippmann

    Slice assignment for strings?

    Jens Lippmann, May 25, 2004, in forum: Python
    Replies:
    4
    Views:
    467
    Paul Rubin
    May 25, 2004
  5. Andrew
    Replies:
    2
    Views:
    246
    Stephen Horne
    Sep 21, 2008
Loading...

Share This Page