470,648 Members | 1,571 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

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 2844


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Mark | last post: by
18 posts views Thread by Toto | last post: by
15 posts views Thread by Beeeeeves | last post: by
12 posts views Thread by amer.terzic | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.