473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Character Manipulation

I need to iterate characters in a loop. For example, all the letter from
a to r. In C I would just do that:
char c;
for (c='a'; c<='r'; c++)

It doesn't work in JS.

Thanks.
Jul 23 '05 #1
9 2930


Adelson Anton wrote:
I need to iterate characters in a loop. For example, all the letter from
a to r. In C I would just do that:
char c;
for (c='a'; c<='r'; c++)

It doesn't work in JS.


Use 'charcater'.charCodeAt(0) to find the Unicode character code of a
character and String.fromCharCode(chacterCode) to find the character
having a certain character code:

var startCharacter = 'a';
var endCharacter = 'r';
var startCode = startCharacter.charCodeAt(0);
var endCode = endCharacter.charCodeAt(0);
var result = '';
for (var i = startCode; i <= endCode; i++) {
result += String.fromCharCode(i) + '; ';
}
alert(result)

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2

"Adelson Anton" <ad*****@mail.ru> wrote in message news:40********@duster.adelaide.on.net...
I need to iterate characters in a loop. For example, all the letter from
a to r. In C I would just do that:
char c;
for (c='a'; c<='r'; c++)
It is just a little more convoluted:

for (c = 'a'.charCodeAt(0);c <= 'r'.charCodeAt(0);c++)

It doesn't work in JS.

Thanks.

Jul 23 '05 #3
Where can I find more methods which are present in Character object (if
it's even called like that)?

MikeB wrote:
It is just a little more convoluted:

for (c = 'a'.charCodeAt(0);c <= 'r'.charCodeAt(0);c++)


Jul 23 '05 #4


Adelson Anton wrote:
Where can I find more methods which are present in Character object (if
it's even called like that)?


JavaScript only knows strings, a character is simply represented as a
string with length one.
As for the documentation try
http://devedge.netscape.com/library/...g.html#1193137

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #5
On Sat, 24 Apr 2004 09:42:56 -0500, MikeB <m.byerleyATVerizonDottieNettie>
wrote:

[snip]
for (c = 'a'.charCodeAt(0);c <= 'r'.charCodeAt(0);c++)


The second function call should be taken outside the loop. The "condition"
expression will be evaluated on each loop iteration, which means you're
adding an extra function call for no reason. Whilst this particular case
won't have much impact, re-evaluating object properties or repeatedly
calling functions can introduce a relatively significant overhead.
Invariant optimisation is good habit to get into.

Mike
Please trim your quotes.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #6
> > for (c = 'a'.charCodeAt(0);c <= 'r'.charCodeAt(0);c++)

The second function call should be taken outside the loop. The "condition"
expression will be evaluated on each loop iteration, which means you're
adding an extra function call for no reason. --
I just barely remember enough C (Hey. I just turned 60!) to interpret the question and wanted to
juxtapose the respective elements for the OP.

But your point is "Spot ON"...

Mike
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

Jul 23 '05 #7
Thanks everyone who helped. It works perfectly.

Adelson Anton wrote:
I need to iterate characters in a loop. For example, all the letter from
a to r. In C I would just do that:
char c;
for (c='a'; c<='r'; c++)

It doesn't work in JS.

Thanks.

Jul 23 '05 #8
<url:
http://devedge.netscape.com/library/...ce/frames.html
/> has good documentation of the language and the Netscape 4 DOM
<url:
http://msdn.microsoft.com/workshop/a...ence_entry.asp
/> documents the IE DOM (but not JScript)
<url: http://www.mozilla.org/docs/dom/domref/ /> Gecho-based browser DOM
documentation
<url:
http://msdn.microsoft.com/library/en...ereference.asp
/> JScript documentation (but quite frankly most of the language syntax is
identical to the JavaScript 1.3 Netscape documentation with the exception
of the Netscape specific DOM extensions, so I tend to use that for language
documentation)

Anyway, the documentation specific to this problem is available at:

<url:
http://devedge.netscape.com/library/...ce/string.html
/>

Adelson Anton wrote:
Where can I find more methods which are present in Character object (if
it's even called like that)?

MikeB wrote:
It is just a little more convoluted:

for (c = 'a'.charCodeAt(0);c <= 'r'.charCodeAt(0);c++)


--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #9
Martin Honnen wrote:
var startCharacter = 'a';
var endCharacter = 'r';
var startCode = startCharacter.charCodeAt(0);
var endCode = endCharacter.charCodeAt(0);
var result = '';
for (var i = startCode; i <= endCode; i++) {
result += String.fromCharCode(i) + '; ';
}
alert(result)


I would code a bit more compact with less variables:

for (var i = "a".charCodeAt(0), endCode = "r".charCodeAt(0);
i <= endCode;
i++)
{
result += String.fromCharCode(i) + '; ';
}
alert(result);
PointedEars
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen.B | last post by:
Hi All, Many thanks to those who assisted with my previous posting regarding image file manipulation from a scanner - with your help, i have now put together a solid solution to that problem. ...
5
by: Mark | last post by:
Hello, I'm doing some data manipulation using an XML parser and am getting the following error message: Character reference "" is an invalid XML character. Any thoughts upon what this...
1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer...
18
by: Toto | last post by:
Hello, my problem is quite simple to explain. I have the following string: "table+camera" and I want to remove the + sign: "tablecamera". How do i do that ?
15
by: Beeeeeves | last post by:
Is there a quick way to find the index of the first character different in two strings? For instance, if I had the strings "abcdefghijkl" and "abcdefxyz" I would want the return value to be...
4
by: yllar2005 | last post by:
Hi! I'm using Msxml2.ServerXMLHTTP.3.0 to fetch a HTML page on a remote server. The fetched page is then parsed and the information of interest is extracted and send to the client browser. ...
12
by: amer.terzic | last post by:
Here's what my code is supposed to do (it seems to work fine) Take a char* array and remove a first character from it....as simple as that. The mentioned char* array is a 'global' var shared...
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...

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.