Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > String slice assignment

Reply
Thread Tools

String slice assignment

 
 
Ron Garret
Guest
Posts: n/a
 
      04-02-2009
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
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      04-02-2009
Ron Garret <(E-Mail Removed)> 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'

 
Reply With Quote
 
 
 
 
Ron Garret
Guest
Posts: n/a
 
      04-03-2009
In article <(E-Mail Removed)>,
Lasse Reichstein Nielsen <(E-Mail Removed)> wrote:

> Ron Garret <(E-Mail Removed)> 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
 
Reply With Quote
 
Thomas Allen
Guest
Posts: n/a
 
      04-03-2009
On Apr 3, 2:06*am, Ron Garret <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
> *Lasse Reichstein Nielsen <(E-Mail Removed)> wrote:
>
>
>
> > Ron Garret <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explain slice assignment to newb Andrew Python 2 09-21-2008 02:23 AM
Slice assignment for strings? Jens Lippmann Python 4 05-25-2004 08:56 PM
RE: Assignment to slice Rich Krauter Python 6 01-23-2004 05:33 PM
Re: Assignment to slice James Henderson Python 2 01-22-2004 10:35 AM
Assignment to slice Rich Krauter Python 0 01-21-2004 05:01 PM



Advertisments