3 Replies Latest reply on Feb 5, 2007 2:02 PM by Newsgroup_User

    Subtracting 2 characters(Flash MX)

    thunder_storm
      Hi guys,
      I am trying to subtract 2 characters in action script, My code line looks like this : temp=Number(str.charAt(j)-'A');
      trace(temp);
      But everytime i run the command i get NaN as the O/P, such a command works fine in C, what to do to get this working in actionscript?
        • 1. Re: Subtracting 2 characters(Flash MX)
          Level 7
          Hi,

          What does such a thing yield in C?
          Does it substract ASCII codes?
          Then I would look into the charCodeAt() and fromCharCode() methods.

          trace("C" - "A");
          also gives NaN, so converting it to a Number doesn't do much better :)

          HTH,
          Manno

          thunder_storm wrote:
          > Hi guys,
          > I am trying to subtract 2 characters in action script, My code line looks like
          > this : temp=Number(str.charAt(j)-'A');
          > trace(temp);
          > But everytime i run the command i get NaN as the O/P, such a command works
          > fine in C, what to do to get this working in actionscript?
          >

          --
          ----------
          Manno Bult
          http://www.aloft.nl
          manno@xs4all.nl
          • 2. Re: Subtracting 2 characters(Flash MX)
            thunder_storm Level 1
            Such a code in C wud yield the difference between the postition of the two chars in the string array.. eg. str='abcd..'
            so b-c = 1 .. i m not checking this so i may be wrong but from past exp i think its what we get..
            It does not subtract the ascii codes... i know abt the charCode() .. i wud like to know if there is a way i can get the difference in the position of the 2 chars..
            • 3. Re: Subtracting 2 characters(Flash MX)
              Level 7
              Ah!...

              then the indexOf() (together with Math.abs()) method would do:

              "abcd".indexOf("b") == 1
              "abcd".indexOf("d") == 3

              str = "abcd";
              str.indexOf("b") - str.indexOf("d") == -2 (Math.abs(-2) == 2)

              Manno

              thunder_storm wrote:
              > Such a code in C wud yield the difference between the postition of the two
              > chars in the string array.. eg. str='abcd..'
              > so b-c = 1 .. i m not checking this so i may be wrong but from past exp i
              > think its what we get..
              > It does not subtract the ascii codes... i know abt the charCode() .. i wud
              > like to know if there is a way i can get the difference in the position of the
              > 2 chars..
              >

              --
              ----------
              Manno Bult
              http://www.aloft.nl
              manno@xs4all.nl