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 7 19338
Tom de Neef wrote on 17 feb 2008 in comp.lang.javas cript:
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)
"Evertjan." <ex************ **@interxnl.net schreef in bericht
news:Xn******** ************@19 4.109.133.242.. .
Tom de Neef wrote on 17 feb 2008 in comp.lang.javas cript:
>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
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.cha rAt(P),'x');
--
Bart
In article <47************ ***********@new s.xs4all.nl>, td*****@qolor.n l
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.substrin g() 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.
"Evertjan." wrote:
Bart Van der Donck wrote on 17 feb 2008 in comp.lang.javas cript:
>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.cha rAt(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
Bart Van der Donck wrote on 18 feb 2008 in comp.lang.javas cript:
"Evertjan." wrote:
>Bart Van der Donck wrote on 17 feb 2008 in comp.lang.javas cript:
>>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.cha rAt(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)
In comp.lang.javas cript message <Xn************ ********@194.10 9.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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 understand.)
|
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 outchar(char *outcharstring)
|
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.
If String1.Length > 0 Then String2 = String1
where String1 = ""
This statement also evaluates as true when String1 = "":
|
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 QA tested
for a new year release. I don't think that going to prove practical, but
there is no harm in trying :) and some serious family commitments. But
it has to be done soon so this is stage one.
Mike Winter provided an extensive list of...
|
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 file of SQL
statements. The reason I need to make the change is the SQL contains
imbedded semicolon characters within text values. There are also
imbedded double-quotes and text values are enclosed within
single-quotes.
As far as I can tell,...
| |
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 SQL statement. Can you help trap
this character not to produce error?
Me
|
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 please tell me how do i go about it?
|
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 Read_bin(ByVal ruta As String)
Dim cadena As String = ""
Dim dato As Array
If File.Exists(ruta) = True Then
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |