reverse of String.charCodeAt ?

K

ken

hello,

i'm using charCodeAt to get the ascii value at a given location in a
string but now i want to set the value of a location in a string to a
specific ascii value. is there a way to do this?

the only thing i can think of is to use the 'replace' method with a
regular expression that matches the nth location in the string and
replaces it with the new ascii value but that seems crude.

i appreciate any suggestions,
thanks!
ken
 
D

Dietmar Meier

ken said:
i'm using charCodeAt to get the ascii value at a given location in a
string but now i want to set the value of a location in a string to a
specific ascii value. is there a way to do this?

function setChar(sString, nPosition, nCharCode) {
if (typeof sString == "string" && sString.length > nPosition) {
sString = sString.substring(0, nPosition) +
String.fromCharCode(nCharCode) +
sString.substring(nPosition + 1);
}
return sString;
}

ciao, dhgm
 
M

McKirahan

ken said:
hello,

i'm using charCodeAt to get the ascii value at a given location in a
string but now i want to set the value of a location in a string to a
specific ascii value. is there a way to do this?

the only thing i can think of is to use the 'replace' method with a
regular expression that matches the nth location in the string and
replaces it with the new ascii value but that seems crude.

i appreciate any suggestions,
thanks!
ken

Will this help? It rebuilds the string.

alert( replaceChar("abcdefghijklmnopqrstuvwxyz","m","M") );

function replaceChar(phrase,char1,char2) {
// Replace first instance of "char1" with "char2" in "phrase".
var i = phrase.indexOf(char1);
var s = "";
if (i >= 0) {
if (i > 0) s = phrase.substring(0,i);
s += char2;
s += phrase.substr(i+1);
return (s);
}
}
 
R

RobB

ken said:
hello,

i'm using charCodeAt to get the ascii value at a given location in a
string but now i want to set the value of a location in a string to a
specific ascii value. is there a way to do this?

the only thing i can think of is to use the 'replace' method with a
regular expression that matches the nth location in the string and
replaces it with the new ascii value but that seems crude.

I beg your pardon...

String.prototype.replaceCharAt = function(pos, charCode)
{
return this.replace(
new RegExp('(.{' + pos + '}).'),
'$1' + String.fromCharCode(charCode)
);
}

x = 'I stepped in that trap.';
x = x.replaceCharAt(18, 99);
alert(x);
 
K

ken

nice. thank you!
ken

Dietmar said:
function setChar(sString, nPosition, nCharCode) {
if (typeof sString == "string" && sString.length > nPosition) {
sString = sString.substring(0, nPosition) +
String.fromCharCode(nCharCode) +
sString.substring(nPosition + 1);
}
return sString;
}

ciao, dhgm
 
T

Thomas 'PointedEars' Lahn

Dietmar said:
function setChar(sString, nPosition, nCharCode) {
if (typeof sString == "string" && sString.length > nPosition) {
sString = sString.substring(0, nPosition) +
String.fromCharCode(nCharCode) +
sString.substring(nPosition + 1);
}
return sString;
}

function setChar(sString, nPosition, nCharCode)
{
if (sString.length > nPosition)
{
var c = String.fromCharCode(nCharCode);

// for JavaScript 1.3+ strings or if 1st arg is a character array
if (typeof sString[0] != "undefined")
{
sString[nPosition] = c;
}
else // ECMAScript 3 compliant string
{
var aString = sString.split("");
aString[nPosition] = c;
sString = aString.join("");
}
}

return sString;
}


PointedEars
 

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,137
Messages
2,570,802
Members
47,349
Latest member
eixataze

Latest Threads

Top