Is there really no more trivial way? I'm looking for the write equivalent of
S.charAt(P).
TIA
Tom
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)
Thank you EJ
Tom
S = S.replace(S.charAt(P),'x');
--
Bart
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
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.
You're right. Trying to adapt my code, I come to exactly the same
result as you.
--
Bart
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.
> 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.