Character Manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adelson Anton

    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.
  • Martin Honnen

    #2
    Re: Character Manipulation



    Adelson Anton wrote:
    [color=blue]
    > 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.[/color]

    Use 'charcater'.cha rCodeAt(0) to find the Unicode character code of a
    character and String.fromChar Code(chacterCod e) to find the character
    having a certain character code:

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

    --

    Martin Honnen


    Comment

    • MikeB

      #3
      Re: Character Manipulation


      "Adelson Anton" <adelson@mail.r u> wrote in message news:408a5267$1 @duster.adelaid e.on.net...[color=blue]
      > 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++)[/color]

      It is just a little more convoluted:

      for (c = 'a'.charCodeAt( 0);c <= 'r'.charCodeAt( 0);c++)
      [color=blue]
      >
      > It doesn't work in JS.
      >
      > Thanks.[/color]


      Comment

      • Adelson Anton

        #4
        Re: Character Manipulation

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

        MikeB wrote:[color=blue]
        > It is just a little more convoluted:
        >
        > for (c = 'a'.charCodeAt( 0);c <= 'r'.charCodeAt( 0);c++)[/color]

        Comment

        • Martin Honnen

          #5
          Re: Character Manipulation



          Adelson Anton wrote:
          [color=blue]
          > Where can I find more methods which are present in Character object (if
          > it's even called like that)?[/color]

          JavaScript only knows strings, a character is simply represented as a
          string with length one.
          As for the documentation try


          --

          Martin Honnen


          Comment

          • Michael Winter

            #6
            Re: Character Manipulation

            On Sat, 24 Apr 2004 09:42:56 -0500, MikeB <m.byerleyATVer izonDottieNetti e>
            wrote:

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

            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.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

            Comment

            • MikeB

              #7
              Re: Character Manipulation

              > > for (c = 'a'.charCodeAt( 0);c <= 'r'.charCodeAt( 0);c++)[color=blue]
              >
              > 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. --[/color]

              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
              [color=blue]
              > Michael Winter
              > M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)[/color]


              Comment

              • Adelson Anton

                #8
                Re: Character Manipulation

                Thanks everyone who helped. It works perfectly.

                Adelson Anton wrote:[color=blue]
                > 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.[/color]

                Comment

                • Grant Wagner

                  #9
                  Re: Character Manipulation

                  <url:

                  /> has good documentation of the language and the Netscape 4 DOM
                  <url:
                  Gain technical skills through documentation and training, earn certifications and connect with the community

                  /> documents the IE DOM (but not JScript)
                  <url: http://www.mozilla.org/docs/dom/domref/ /> Gecho-based browser DOM
                  documentation
                  <url:
                  Gain technical skills through documentation and training, earn certifications and connect with the community

                  /> 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:

                  />

                  Adelson Anton wrote:
                  [color=blue]
                  > Where can I find more methods which are present in Character object (if
                  > it's even called like that)?
                  >
                  > MikeB wrote:[color=green]
                  > > It is just a little more convoluted:
                  > >
                  > > for (c = 'a'.charCodeAt( 0);c <= 'r'.charCodeAt( 0);c++)[/color][/color]

                  --
                  | Grant Wagner <gwagner@agrico reunited.com>

                  * Client-side Javascript and Netscape 4 DOM Reference available at:
                  *


                  * Internet Explorer DOM Reference available at:
                  *
                  Gain technical skills through documentation and training, earn certifications and connect with the community


                  * 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


                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Character Manipulation

                    Martin Honnen wrote:
                    [color=blue]
                    > var startCharacter = 'a';
                    > var endCharacter = 'r';
                    > var startCode = startCharacter. charCodeAt(0);
                    > var endCode = endCharacter.ch arCodeAt(0);
                    > var result = '';
                    > for (var i = startCode; i <= endCode; i++) {
                    > result += String.fromChar Code(i) + '; ';
                    > }
                    > alert(result)[/color]

                    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.fromChar Code(i) + '; ';
                    }
                    alert(result);


                    PointedEars

                    Comment

                    Working...