reverse of String.charCodeAt ?

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

    #1

    reverse of String.charCodeAt ?

    hello,

    i'm using charCodeAt to get the ascii value at a given location in a
    string but now i want to set the value of a location in a string to a
    specific ascii value. is there a way to do this?

    the only thing i can think of is to use the 'replace' method with a
    regular expression that matches the nth location in the string and
    replaces it with the new ascii value but that seems crude.

    i appreciate any suggestions,
    thanks!
    ken
  • Dietmar Meier

    #2
    Re: reverse of String.charCode At ?

    ken wrote:
    [color=blue]
    > i'm using charCodeAt to get the ascii value at a given location in a
    > string but now i want to set the value of a location in a string to a
    > specific ascii value. is there a way to do this?[/color]

    function setChar(sString , nPosition, nCharCode) {
    if (typeof sString == "string" && sString.length > nPosition) {
    sString = sString.substri ng(0, nPosition) +
    String.fromChar Code(nCharCode) +
    sString.substri ng(nPosition + 1);
    }
    return sString;
    }

    ciao, dhgm

    Comment

    • McKirahan

      #3
      Re: reverse of String.charCode At ?

      "ken" <ken@nospam.com > wrote in message
      news:cta3e.8696 45$8l.124412@pd 7tw1no...[color=blue]
      > hello,
      >
      > i'm using charCodeAt to get the ascii value at a given location in a
      > string but now i want to set the value of a location in a string to a
      > specific ascii value. is there a way to do this?
      >
      > the only thing i can think of is to use the 'replace' method with a
      > regular expression that matches the nth location in the string and
      > replaces it with the new ascii value but that seems crude.
      >
      > i appreciate any suggestions,
      > thanks!
      > ken[/color]

      Will this help? It rebuilds the string.

      alert( replaceChar("ab cdefghijklmnopq rstuvwxyz","m", "M") );

      function replaceChar(phr ase,char1,char2 ) {
      // Replace first instance of "char1" with "char2" in "phrase".
      var i = phrase.indexOf( char1);
      var s = "";
      if (i >= 0) {
      if (i > 0) s = phrase.substrin g(0,i);
      s += char2;
      s += phrase.substr(i +1);
      return (s);
      }
      }


      Comment

      • RobB

        #4
        Re: reverse of String.charCode At ?

        ken wrote:[color=blue]
        > hello,
        >
        > i'm using charCodeAt to get the ascii value at a given location in a
        > string but now i want to set the value of a location in a string to a[/color]
        [color=blue]
        > specific ascii value. is there a way to do this?
        >
        > the only thing i can think of is to use the 'replace' method with a
        > regular expression that matches the nth location in the string and
        > replaces it with the new ascii value but that seems crude.[/color]

        I beg your pardon...

        String.prototyp e.replaceCharAt = function(pos, charCode)
        {
        return this.replace(
        new RegExp('(.{' + pos + '}).'),
        '$1' + String.fromChar Code(charCode)
        );
        }

        x = 'I stepped in that trap.';
        x = x.replaceCharAt (18, 99);
        alert(x);

        Comment

        • ken

          #5
          Re: reverse of String.charCode At ?

          nice. thank you!
          ken

          Dietmar Meier wrote:[color=blue]
          > ken wrote:
          >[color=green]
          >> i'm using charCodeAt to get the ascii value at a given location in a
          >> string but now i want to set the value of a location in a string to a
          >> specific ascii value. is there a way to do this?[/color]
          >
          >
          > function setChar(sString , nPosition, nCharCode) {
          > if (typeof sString == "string" && sString.length > nPosition) {
          > sString = sString.substri ng(0, nPosition) +
          > String.fromChar Code(nCharCode) +
          > sString.substri ng(nPosition + 1);
          > }
          > return sString;
          > }
          >
          > ciao, dhgm[/color]

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: reverse of String.charCode At ?

            Dietmar Meier wrote:
            [color=blue]
            > ken wrote:[color=green]
            >> i'm using charCodeAt to get the ascii value at a given location in a
            >> string but now i want to set the value of a location in a string to a
            >> specific ascii value. is there a way to do this?[/color]
            >
            > function setChar(sString , nPosition, nCharCode) {
            > if (typeof sString == "string" && sString.length > nPosition) {
            > sString = sString.substri ng(0, nPosition) +
            > String.fromChar Code(nCharCode) +
            > sString.substri ng(nPosition + 1);
            > }
            > return sString;
            > }[/color]

            function setChar(sString , nPosition, nCharCode)
            {
            if (sString.length > nPosition)
            {
            var c = String.fromChar Code(nCharCode) ;

            // for JavaScript 1.3+ strings or if 1st arg is a character array
            if (typeof sString[0] != "undefined" )
            {
            sString[nPosition] = c;
            }
            else // ECMAScript 3 compliant string
            {
            var aString = sString.split(" ");
            aString[nPosition] = c;
            sString = aString.join("" );
            }
            }

            return sString;
            }


            PointedEars

            Comment

            Working...