473,598 Members | 3,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reverse of String.charCode At ?

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
5 7652
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.substri ng(0, nPosition) +
String.fromChar Code(nCharCode) +
sString.substri ng(nPosition + 1);
}
return sString;
}

ciao, dhgm
Jul 23 '05 #2
"ken" <ke*@nospam.com > wrote in message
news:cta3e.8696 45$8l.124412@pd 7tw1no...
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("ab cdefghijklmnopq rstuvwxyz","m", "M") );

function replaceChar(phr ase,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.substrin g(0,i);
s += char2;
s += phrase.substr(i +1);
return (s);
}
}
Jul 23 '05 #3
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.prototyp e.replaceCharAt = function(pos, charCode)
{
return this.replace(
new RegExp('(.{' + pos + '}).'),
'$1' + String.fromChar Code(charCode)
);
}

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

Jul 23 '05 #4
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.substri ng(0, nPosition) +
String.fromChar Code(nCharCode) +
sString.substri ng(nPosition + 1);
}
return sString;
}

ciao, dhgm

Jul 23 '05 #5
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.substri ng(0, nPosition) +
String.fromChar Code(nCharCode) +
sString.substri ng(nPosition + 1);
}
return sString;
}


function setChar(sString , nPosition, nCharCode)
{
if (sString.length > nPosition)
{
var c = String.fromChar Code(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4877
by: Laurent Vogel | last post by:
hello, I'm writing a script which needs about 60 kbytes of text data. Currently it looks like: function1("small string1"); function2("small string2"); function1("small string3"); and so on. In order to have it loaded more quickly, I'm considering actually puting all the data in one big string (something like: 1<small string1>2<small string2>1<small string3>...
4
6206
by: Steve | last post by:
Hi, I am trying to do a very simple "encryption" of a text string in java script. For instance, if the user enters : steve, I want to just convert each character to its ASCII value and add 5 to each character, then convert back to a string giving: "xyj{j" for this example. is there a simple way to do this? any suggestions on functions I can use would be greatful!
3
2662
by: Peter | last post by:
Hi, I try to make up a javascript string which contains numeric numbers in any positions. For example, I want to make a string: secretcode, where secretcode.charAt(0)==(-21), secretcode.charAt(5)==(-14). Is there any way to accomplish this task? Thanks in advance, Peter
14
12331
by: Mr.Clean | last post by:
I have a string. I'd like to take this string and make each char change until it gets to another char at the same position of the string. Example: Original String: " NEW YORK " Final String: "SAN FRANSISCO" I'd like each char to loop through the chars on a timer
9
2274
by: Velvet | last post by:
I'm trying to convert some JavaScript to C# and don't know how to get the character code of a character in a string. in JavaScript it is as follows: for( i = 0; i < email.length; i++) { var fs = email.charCodeAt(i); uniemail = uniemail + '&#' + fs + ';';
11
5059
by: Brian | last post by:
I have been working on a data reception system.. I am still finding my way around Javascript, though I am accomplishing much. I just fixed a flaw that was really hard to find. The symptoms are this: I get a multiline string returned to Javascript from a Proxy+Google Maps API GDownloadUrl() The data, when added to a DOM table looked fine, about 20 lines in CSV format
20
3688
by: Xcriber51 | last post by:
Hi -- I'm not entirely familiar with the norms and standard libraries of JavaScript so if the answer to this is yesterday's news, please ignore. I'm trying to write a simple text formatting function to make headings proper case -- i.e. first letter of words capitalized. I first tried this...
10
8686
by: czechboy | last post by:
Hi, I would like to use charCodeAt function but it returns wrong dec. numbers. The question is how to set character of the js. file executed? I can not use any kind of tags (<script<metaetc) since the js. file is executed directly through the client server. So no html etc... is there any solution? I need to display utf-8 text in RTF1 so I need to transfer the text to unicode by charCodeAt so that is why I needed it. Here is the function...
2
14763
by: ziycon | last post by:
Is there a function that does the opposite of charCodeAt(), I've writing a basic password tool and i can turn the text into unicode no problem but I can't return the unicode back to text?
0
7987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7899
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8392
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8397
jinu1996
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...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
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();...
0
3897
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...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1250
bsmnconsultancy
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...

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.