473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Change a single character in a string

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
Feb 17 '08 #1
7 19313
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>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 17 '08 #2
"Evertjan." <ex**************@interxnl.netschreef in bericht
news:Xn********************@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
Feb 17 '08 #3
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');

--
Bart
Feb 17 '08 #4
In article <47***********************@news.xs4all.nl>, td*****@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.
Feb 17 '08 #5
"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
Feb 18 '08 #6
Bart Van der Donck wrote on 18 feb 2008 in comp.lang.javascript:
"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.
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('');
};
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 18 '08 #7
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Mon, 18 Feb 2008 08:01:18, Evertjan. <ex**************@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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 19 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
12
by: Dennis Plöger | last post by:
Hi all! I'm currently having some problems parsing a char array in c++. (And yes, I'm a half-newbie ;-)) Perhaps you can help me with this: #include <iostream> using std::cout; void...
6
by: DLP22192 | last post by:
I have the following single-line if statement that is evaluating true even though it shouldn't. I have never seen this before and I am concerned that this can happen in other areas of my code. ...
4
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an...
5
by: Mortimer | last post by:
Hi, I hope someone can help. I can't seem to find the answer to this anywhere. I need to change the SQL terminator from a semicolon (;) to an exclamation point within SPUFI in order to process a...
9
by: M P | last post by:
Hi! I am looking for a way that I can trap the single quotation mark. If an encoder uses single quotation mark on a textbox field, it always give me an error because I use single quotes on the...
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.