Change a single character in a string

T

Tom de Neef

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
 
E

Evertjan.

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.

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

Tom de Neef

Evertjan. said:
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
 
B

Bart Van der Donck

Tom said:
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');
 
E

Evertjan.

Bart Van der Donck wrote on 17 feb 2008 in comp.lang.javascript:
Tom said:
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.

<script type='text/javascript'>

P=4;
S='baaaaaa';
S = S.replace(S.charAt(P),'x');
document.write( S ); // bxaaaaa

// The OP wanted: baaaxaa


</script>
 
T

T. Rex

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

Bart Van der Donck

Evertjan. said:
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.
 
E

Evertjan.

Bart Van der Donck wrote on 18 feb 2008 in comp.lang.javascript:
Evertjan. said:
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.

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('');
};
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
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.
 
E

Evertjan.

Dr J R Stockton wrote on 19 feb 2008 in comp.lang.javascript:
In comp.lang.javascript message
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top