Replace a char at a postion in a String with another

R

Ron Brennan

Good evening.

I'd like to replace whatever character is aways at the postion 2 in a String
with another character. For example, the y in wxyx with b.

I'm sure it must be possible, but I'll be damned if I can find out how.

Thanks,
Ron.
 
R

Randy Webb

Ron said:
Good evening.

I'd like to replace whatever character is aways at the postion 2 in a String
with another character. For example, the y in wxyx with b.

I'm sure it must be possible, but I'll be damned if I can find out how.

Thanks,
Ron.

var myString="abcdef";
var replaceCharacter = "J";
var replacePosition = 2;
var outputVar = myString.substring(0,replacePosition) +
replaceCharacter +
myString.substring(replacePosition+1,myString.length);
alert(outputVar)

Probably easier, more efficient ways, to do that, but that one does what
you asked. It takes a substring up to the point you want to lose, takes
whats after it, then it puts it back together.

You could also use .replace:
<URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthreplace.asp
/>
 
R

Ron Brennan

Randy Webb said:
var myString="abcdef";
var replaceCharacter = "J";
var replacePosition = 2;
var outputVar = myString.substring(0,replacePosition) +
replaceCharacter +
myString.substring(replacePosition+1,myString.length);
alert(outputVar)

Probably easier, more efficient ways, to do that, but that one does what
you asked. It takes a substring up to the point you want to lose, takes
whats after it, then it puts it back together.

You could also use .replace:
<URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/ht
ml/js56jsmthreplace.asp
/>

I'll have to use Randy's method if I have to, but I suspect that
regexp.replace can accomplish the task elegantly. Anyone know if this is
even possible?
 
M

Michael Winter

[snip]
I'll have to use Randy's method if I have to, but I suspect that
regexp.replace can accomplish the task elegantly. Anyone know if this is
even possible?

(Very) briefly tested:

<string>.replace(/^(..)./, '$1' + <character>)

So, from your example:

'wxyx'.replace(/^(..)./, '$1b'); // 'wxbx'

Mike
 
E

Evertjan.

Ron Brennan wrote on 30 okt 2004 in comp.lang.javascript:
I'd like to replace whatever character is aways at the postion 2 in a
String with another character. For example, the y in wxyx with b.

counting from 0
I'm sure it must be possible, but I'll be damned if I can find out
how.

s = "wxyx"

a = s.split("")

a[2]="b"

s = a.join("")

alert(s)
 
R

Ron Brennan

The replace solution seems best for my problem although the others will
solve it too. Thanks to everybody.
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top