By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

reverse of String.charCodeAt ?

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ken wrote:
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
Jul 23 '05 #2

P: n/a
"ken" <ke*@nospam.com> wrote in message
news:cta3e.869645$8l.124412@pd7tw1no...
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);
}
}
Jul 23 '05 #3

P: n/a
ken wrote:
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);

Jul 23 '05 #4

P: n/a
ken
nice. thank you!
ken

Dietmar Meier wrote:
ken wrote:
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

Jul 23 '05 #5

P: n/a
Dietmar Meier wrote:
ken wrote:
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;
}


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
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.