Character Manipulation

A

Adelson Anton

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.
 
M

Martin Honnen

Adelson said:
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)
 
M

MikeB

Adelson Anton said:
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++)
 
A

Adelson Anton

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

Michael Winter

[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.
 
M

MikeB

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
 
G

Grant Wagner

<url:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
/> has good documentation of the language and the Netscape 4 DOM
<url:
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-us/script56/html/js56jslrfjscriptlanguagereference.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/manuals/2000/javascript/1.3/reference/string.html
/>

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

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
T

Thomas 'PointedEars' Lahn

Martin said:
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
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top