Change a single character in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom de Neef

    Change a single character in a string

    I need to change one character at a known position in a string.
    In Pascal I would change the P's character of a string S into 'x' with
    S[P]:='x';
    In JavaScript I come no further than S = S.substr(0,P-2)+'x'+S.substr (P)

    Is there really no more trivial way? I'm looking for the write equivalent of
    S.charAt(P).
    TIA
    Tom


  • Evertjan.

    #2
    Re: Change a single character in a string

    Tom de Neef wrote on 17 feb 2008 in comp.lang.javas cript:
    I need to change one character at a known position in a string.
    In Pascal I would change the P's character of a string S into 'x'
    with
    S[P]:='x';
    In JavaScript I come no further than S =
    S.substr(0,P-2)+'x'+S.substr (P)
    >
    Is there really no more trivial way? I'm looking for the write
    equivalent of S.charAt(P).
    A string cannot be changed in JS, only replaced.

    <script type='text/javascript'>

    function replaceOneChar( s,c,n){
    var re = new RegExp('^(.{'+ --n +'}).(.*)$','') ;
    return s.replace(re,'$ 1'+c+'$2');
    };

    alert( replaceOneChar( 'abcde','X',3) ); // abXde

    </script>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Tom de Neef

      #3
      Re: Change a single character in a string

      "Evertjan." <exjxw.hannivoo rt@interxnl.net schreef in bericht
      news:Xns9A47A1B 795B32eejj99@19 4.109.133.242.. .
      Tom de Neef wrote on 17 feb 2008 in comp.lang.javas cript:
      >
      >I need to change one character at a known position in a string.
      >In Pascal I would change the P's character of a string S into 'x'
      with
      >S[P]:='x';
      >In JavaScript I come no further than S =
      >S.substr(0,P-2)+'x'+S.substr (P)
      >>
      >Is there really no more trivial way? I'm looking for the write
      >equivalent of S.charAt(P).
      >
      A string cannot be changed in JS, only replaced.
      >
      Thank you EJ
      Tom


      Comment

      • Bart Van der Donck

        #4
        Re: Change a single character in a string

        Tom de Neef wrote:
        I need to change one character at a known position in a string.
        In Pascal I would change the P's character of a string S into 'x' with
        S[P]:='x';
        In JavaScript I come no further than S = S.substr(0,P-2)+'x'+S.substr (P)
        Is there really no more trivial way? I'm looking for the write equivalent of
        S.charAt(P).
        S = S.replace(S.cha rAt(P),'x');

        --
        Bart

        Comment

        • T. Rex

          #5
          Re: Change a single character in a string

          In article <47b8466e$0$143 54$e4fe514c@new s.xs4all.nl>, tdeneef@qolor.n l
          says...
          I need to change one character at a known position in a string.
          In Pascal I would change the P's character of a string S into 'x' with
          S[P]:='x';
          In JavaScript I come no further than S = S.substr(0,P-2)+'x'+S.substr (P)
          You couldn't possibly have tried that, and found it even remotely close
          to satisfactory. The second parameter to string.substr() is a length
          parameter, not a position index.

          As coded, that will:
          a) fail, if P < 2
          b) delete character P-2 and replace character P-1 with 'x', if P >= 2

          If you want to use a position index instead of a length, look at
          string.substrin g() or string.slice().
          Is there really no more trivial way? I'm looking for the write equivalent of
          S.charAt(P).
          If by that you mean some means of modifying it directly as in Pascal --
          no.

          Comment

          • Bart Van der Donck

            #6
            Re: Change a single character in a string

            "Evertjan." wrote:
            Bart Van der Donck wrote on 17 feb 2008 in comp.lang.javas cript:
            >
            >Tom de Neef wrote:
            >
            >>I need to change one character at a known position in a string.
            >>In Pascal I would change the P's character of a string S into 'x'
            >>with S[P]:='x';
            >>In JavaScript I come no further than S =
            >>S.substr(0, P-2)+'x'+S.substr (P) Is there really no more trivial way?
            >>I'm looking for the write equivalent of S.charAt(P).
            >
            >S = S.replace(S.cha rAt(P),'x');
            >
            No that would not work right, Bart,
            as it would replace the first appearance of that letter.
            You're right. Trying to adapt my code, I come to exactly the same
            result as you.

            --
            Bart

            Comment

            • Evertjan.

              #7
              Re: Change a single character in a string

              Bart Van der Donck wrote on 18 feb 2008 in comp.lang.javas cript:
              "Evertjan." wrote:
              >Bart Van der Donck wrote on 17 feb 2008 in comp.lang.javas cript:
              >>
              >>Tom de Neef wrote:
              >>
              >>>I need to change one character at a known position in a string.
              >>>In Pascal I would change the P's character of a string S into 'x'
              >>>with S[P]:='x';
              >>>In JavaScript I come no further than S =
              >>>S.substr(0 ,P-2)+'x'+S.substr (P) Is there really no more trivial way?
              >>>I'm looking for the write equivalent of S.charAt(P).
              >>
              >>S = S.replace(S.cha rAt(P),'x');
              >>
              >No that would not work right, Bart,
              >as it would replace the first appearance of that letter.
              >
              You're right. Trying to adapt my code, I come to exactly the same
              result as you.
              I already gave this regex in another branch of this tread:

              function replaceOneChar( s,c,n){
              var re = new RegExp('^(.{'+ --n +'}).(.*)$','') ;
              return s.replace(re,'$ 1'+c+'$2');
              };

              A non regex solution would be:

              function replaceOneChar( s,c,n){
              (s = s.split(''))[--n] = c;
              return s.join('');
              };


              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • Dr J R Stockton

                #8
                Re: Change a single character in a string

                In comp.lang.javas cript message <Xns9A485BC5FA2 D8eejj99@194.10 9.133.242>
                , Mon, 18 Feb 2008 08:01:18, Evertjan. <exjxw.hannivoo rt@interxnl.net >
                posted:
                >
                >I already gave this regex in another branch of this tread:
                >
                >function replaceOneChar( s,c,n){
                var re = new RegExp('^(.{'+ --n +'}).(.*)$','') ;
                return s.replace(re,'$ 1'+c+'$2');
                >};
                >
                >A non regex solution would be:
                >
                >function replaceOneChar( s,c,n){
                (s = s.split(''))[--n] = c;
                return s.join('');
                >};
                There is an overhead to the construction of a RegExp and to the
                commencement of each use, but after that the scanning and replacement
                will be reasonably fast.

                Method split requires the creation of a number of Objects for short-term
                use, but after that the replacement will be quick.

                With XP sp2 IE6, I find that the two methods are of similar speed for
                8-character strings; for a 2-character string, RegExp takes about half
                as long again as split; for a 30-character string, split takes about
                twice as long as RegExp; for a 90-character string, split takes over
                five times as long as RegExp.

                --
                (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE6 IE7 FF2 Op9 Sf3
                news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
                <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                Working...