Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Change a single character in a string

0 views
Skip to first unread message

Tom de Neef

unread,
Feb 17, 2008, 9:36:28 AM2/17/08
to
I need to change one character at a known position in a string.
In Pascal I would change the P's character of a string S into 'x' with
S[P]:='x';
In JavaScript I come no further than S = S.substr(0,P-2)+'x'+S.substr(P)

Is there really no more trivial way? I'm looking for the write equivalent of
S.charAt(P).
TIA
Tom


Evertjan.

unread,
Feb 17, 2008, 9:53:51 AM2/17/08
to

A string cannot be changed in JS, only replaced.

<script type='text/javascript'>

function replaceOneChar(s,c,n){
var re = new RegExp('^(.{'+ --n +'}).(.*)$','');
return s.replace(re,'$1'+c+'$2');
};

alert( replaceOneChar('abcde','X',3) ); // abXde

</script>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Tom de Neef

unread,
Feb 17, 2008, 10:15:08 AM2/17/08
to
"Evertjan." <exjxw.ha...@interxnl.net> schreef in bericht
news:Xns9A47A1B7...@194.109.133.242...

> Tom de Neef wrote on 17 feb 2008 in comp.lang.javascript:
>
>> I need to change one character at a known position in a string.
>> In Pascal I would change the P's character of a string S into 'x'
> with
>> S[P]:='x';
>> In JavaScript I come no further than S =
>> S.substr(0,P-2)+'x'+S.substr(P)
>>
>> Is there really no more trivial way? I'm looking for the write
>> equivalent of S.charAt(P).
>
> A string cannot be changed in JS, only replaced.
>

Thank you EJ
Tom


Bart Van der Donck

unread,
Feb 17, 2008, 4:02:10 PM2/17/08
to
Tom de Neef wrote:

S = S.replace(S.charAt(P),'x');

--
Bart

Evertjan.

unread,
Feb 17, 2008, 4:23:23 PM2/17/08
to

No that would not work right, Bart,
as it would replace the first appearance of that letter.

<script type='text/javascript'>

P=4;
S='baaaaaa';


S = S.replace(S.charAt(P),'x');

document.write( S ); // bxaaaaa

// The OP wanted: baaaxaa

T. Rex

unread,
Feb 17, 2008, 5:46:33 PM2/17/08
to
In article <47b8466e$0$14354$e4fe...@news.xs4all.nl>, tde...@qolor.nl
says...

> I need to change one character at a known position in a string.
> In Pascal I would change the P's character of a string S into 'x' with
> S[P]:='x';
> In JavaScript I come no further than S = S.substr(0,P-2)+'x'+S.substr(P)

You couldn't possibly have tried that, and found it even remotely close
to satisfactory. The second parameter to string.substr() is a length
parameter, not a position index.

As coded, that will:
a) fail, if P < 2
b) delete character P-2 and replace character P-1 with 'x', if P >= 2

If you want to use a position index instead of a length, look at
string.substring() or string.slice().

> Is there really no more trivial way? I'm looking for the write equivalent of
> S.charAt(P).

If by that you mean some means of modifying it directly as in Pascal --
no.

Bart Van der Donck

unread,
Feb 18, 2008, 2:49:51 AM2/18/08
to
"Evertjan." wrote:
> Bart Van der Donck wrote on 17 feb 2008 in comp.lang.javascript:
>
>> Tom de Neef wrote:
>
>>> I need to change one character at a known position in a string.
>>> In Pascal I would change the P's character of a string S into 'x'
>>> with S[P]:='x';
>>> In JavaScript I come no further than S =
>>> S.substr(0,P-2)+'x'+S.substr(P) Is there really no more trivial way?
>>> I'm looking for the write equivalent of S.charAt(P).
>
>> S = S.replace(S.charAt(P),'x');
>
> No that would not work right, Bart,
> as it would replace the first appearance of that letter.

You're right. Trying to adapt my code, I come to exactly the same
result as you.

--
Bart

Evertjan.

unread,
Feb 18, 2008, 3:01:18 AM2/18/08
to

I already gave this regex in another branch of this tread:

function replaceOneChar(s,c,n){
var re = new RegExp('^(.{'+ --n +'}).(.*)$','');
return s.replace(re,'$1'+c+'$2');
};

A non regex solution would be:

function replaceOneChar(s,c,n){
(s = s.split(''))[--n] = c;
return s.join('');
};

Dr J R Stockton

unread,
Feb 19, 2008, 6:14:31 AM2/19/08
to
In comp.lang.javascript message <Xns9A485BC5...@194.109.133.242>
, Mon, 18 Feb 2008 08:01:18, Evertjan. <exjxw.ha...@interxnl.net>
posted:

>
>I already gave this regex in another branch of this tread:
>
>function replaceOneChar(s,c,n){
> var re = new RegExp('^(.{'+ --n +'}).(.*)$','');
> return s.replace(re,'$1'+c+'$2');
>};
>
>A non regex solution would be:
>
>function replaceOneChar(s,c,n){
> (s = s.split(''))[--n] = c;
> return s.join('');
>};

There is an overhead to the construction of a RegExp and to the
commencement of each use, but after that the scanning and replacement
will be reasonably fast.

Method split requires the creation of a number of Objects for short-term
use, but after that the replacement will be quick.

With XP sp2 IE6, I find that the two methods are of similar speed for
8-character strings; for a 2-character string, RegExp takes about half
as long again as split; for a 30-character string, split takes about
twice as long as RegExp; for a 90-character string, split takes over
five times as long as RegExp.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE6 IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Evertjan.

unread,
Feb 19, 2008, 3:27:33 PM2/19/08
to
Dr J R Stockton wrote on 19 feb 2008 in comp.lang.javascript:

> In comp.lang.javascript message
> <Xns9A485BC5...@194.109.133.242> , Mon, 18 Feb 2008 08:01:18,
> Evertjan. <exjxw.ha...@interxnl.net> posted:
>>
>>I already gave this regex in another branch of this tread:
>>
>>function replaceOneChar(s,c,n){
>> var re = new RegExp('^(.{'+ --n +'}).(.*)$','');
>> return s.replace(re,'$1'+c+'$2');
>>};
>>
>>A non regex solution would be:
>>
>>function replaceOneChar(s,c,n){
>> (s = s.split(''))[--n] = c;
>> return s.join('');
>>};
>
> There is an overhead to the construction of a RegExp and to the
> commencement of each use, but after that the scanning and replacement
> will be reasonably fast.
>
> Method split requires the creation of a number of Objects for
> short-term use, but after that the replacement will be quick.
>
> With XP sp2 IE6, I find that the two methods are of similar speed for
> 8-character strings; for a 2-character string, RegExp takes about half
> as long again as split; for a 30-character string, split takes about
> twice as long as RegExp; for a 90-character string, split takes over
> five times as long as RegExp.

The array method of sting manipulation is so versatile.

You could want to have a substing letter 4 to excl. 8 reversed:

var s = 'abcdefghijk';
n1 = 4; n1--;
n2 = 8; n2--;
s = s.split('');
s = [].concat(s.slice(0,n1),s.slice(n1,n2).reverse(),s.slice(n2));
s = s.join('');
document.write(s);

Can be done with regex, but not that intuitive.

0 new messages