Replace a char at a postion in a String with another

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

    Replace a char at a postion in a String with another

    Good evening.

    I'd like to replace whatever character is aways at the postion 2 in a String
    with another character. For example, the y in wxyx with b.

    I'm sure it must be possible, but I'll be damned if I can find out how.

    Thanks,
    Ron.



  • Randy Webb

    #2
    Re: Replace a char at a postion in a String with another

    Ron Brennan wrote:
    [color=blue]
    > Good evening.
    >
    > I'd like to replace whatever character is aways at the postion 2 in a String
    > with another character. For example, the y in wxyx with b.
    >
    > I'm sure it must be possible, but I'll be damned if I can find out how.
    >
    > Thanks,
    > Ron.[/color]

    var myString="abcde f";
    var replaceCharacte r = "J";
    var replacePosition = 2;
    var outputVar = myString.substr ing(0,replacePo sition) +
    replaceCharacte r +
    myString.substr ing(replacePosi tion+1,myString .length);
    alert(outputVar )

    Probably easier, more efficient ways, to do that, but that one does what
    you asked. It takes a substring up to the point you want to lose, takes
    whats after it, then it puts it back together.

    You could also use .replace:
    <URL:

    />
    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • Ron Brennan

      #3
      Re: Replace a char at a postion in a String with another


      "Randy Webb" <HikksNotAtHome @aol.com> wrote in message
      news:lI-dnUo0LqEQvRncRV n-hg@comcast.com. ..[color=blue]
      > Ron Brennan wrote:
      >[color=green]
      > > Good evening.
      > >
      > > I'd like to replace whatever character is aways at the postion 2 in a[/color][/color]
      String[color=blue][color=green]
      > > with another character. For example, the y in wxyx with b.
      > >
      > > I'm sure it must be possible, but I'll be damned if I can find out how.
      > >
      > > Thanks,
      > > Ron.[/color]
      >
      > var myString="abcde f";
      > var replaceCharacte r = "J";
      > var replacePosition = 2;
      > var outputVar = myString.substr ing(0,replacePo sition) +
      > replaceCharacte r +
      > myString.substr ing(replacePosi tion+1,myString .length);
      > alert(outputVar )
      >
      > Probably easier, more efficient ways, to do that, but that one does what
      > you asked. It takes a substring up to the point you want to lose, takes
      > whats after it, then it puts it back together.
      >
      > You could also use .replace:
      > <URL:
      >[/color]

      ml/js56jsmthreplac e.asp[color=blue]
      > />
      > --
      > Randy
      > comp.lang.javas cript FAQ - http://jibbering.com/faq[/color]

      I'll have to use Randy's method if I have to, but I suspect that
      regexp.replace can accomplish the task elegantly. Anyone know if this is
      even possible?



      Comment

      • Michael Winter

        #4
        Re: Replace a char at a postion in a String with another

        On Sat, 30 Oct 2004 19:20:02 -0400, Ron Brennan <rbrennan@magma .ca> wrote:

        [snip]
        [color=blue]
        > I'll have to use Randy's method if I have to, but I suspect that
        > regexp.replace can accomplish the task elegantly. Anyone know if this is
        > even possible?[/color]

        (Very) briefly tested:

        <string>.replac e(/^(..)./, '$1' + <character>)

        So, from your example:

        'wxyx'.replace(/^(..)./, '$1b'); // 'wxbx'

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Evertjan.

          #5
          Re: Replace a char at a postion in a String with another

          Ron Brennan wrote on 30 okt 2004 in comp.lang.javas cript:
          [color=blue]
          > I'd like to replace whatever character is aways at the postion 2 in a
          > String with another character. For example, the y in wxyx with b.[/color]

          counting from 0
          [color=blue]
          > I'm sure it must be possible, but I'll be damned if I can find out
          > how.
          >[/color]

          s = "wxyx"

          a = s.split("")

          a[2]="b"

          s = a.join("")

          alert(s)

          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress,
          but let us keep the discussions in the newsgroup)

          Comment

          • Ron Brennan

            #6
            Re: Replace a char at a postion in a String with another

            The replace solution seems best for my problem although the others will
            solve it too. Thanks to everybody.


            Comment

            Working...