Loading ...
Sorry, an error occurred while loading the content.
 

Replacing a character in a string

Expand Messages
  • Spiros Bousbouras
    let s = qwerty Let s say I want to replace the first character by Q. I thought the following would work let s[0] = Q but it gives E689: Can only index a
    Message 1 of 5 , Apr 2, 2009
      let s = 'qwerty'
      Let's say I want to replace the first character by Q. I thought
      the following would work
      let s[0] = 'Q' but it gives
      E689: Can only index a List or Dictionary
      So what is the simplest way to achieve such a task ? I know
      there is the substitute function but I wouldn't want to deal
      with its complexities for such a simple job. The best solution I
      can come up with is

      function Repl_in_str(str, pos, ch)
      if a:pos >= len(a:str)
      echoerr "pos too large"
      return a:str
      endif
      return strpart(a:str,0,a:pos) . a:ch . strpart(a:str , a:pos + 1)
      endfunction

      so I can do let s = Repl_in_str(s , 0 , 'Q')

      Anyone got a better idea ?

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Christian Brabandt
      Hi Spiros! ... let s=substitute(s, ^. , Q , ) or in your case tr() may be even simpler: let s=tr(s, q , Q ) or you could use strpart: let s= Q .strpart(s,1)
      Message 2 of 5 , Apr 2, 2009
        Hi Spiros!

        On Do, 02 Apr 2009, Spiros Bousbouras wrote:

        >
        > let s = 'qwerty'
        > Let's say I want to replace the first character by Q. I thought
        > the following would work
        > let s[0] = 'Q' but it gives
        > E689: Can only index a List or Dictionary
        > So what is the simplest way to achieve such a task ? I know

        let s=substitute(s,'^.','Q', '')

        or in your case tr() may be even simpler:

        let s=tr(s,'q','Q')

        or you could use strpart:

        let s='Q'.strpart(s,1)

        regards,
        Christian
        --
        hundred-and-one symptoms of being an internet addict:
        178. You look for an icon to double-click to open your bedroom window.

        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Tim Chase
        ... You can try: let s = qwerty let pos = 3 let ch = X let s = substitute(s, % .pos. c. , ch, ) though I m not sure it s a great improvement. (might
        Message 3 of 5 , Apr 2, 2009
          Spiros Bousbouras wrote:
          > let s = 'qwerty'
          > Let's say I want to replace the first character by Q. I thought
          > the following would work
          > let s[0] = 'Q' but it gives
          > E689: Can only index a List or Dictionary
          > So what is the simplest way to achieve such a task ? I know
          > there is the substitute function but I wouldn't want to deal
          > with its complexities for such a simple job. The best solution I
          > can come up with is
          >
          > function Repl_in_str(str, pos, ch)
          > if a:pos >= len(a:str)
          > echoerr "pos too large"
          > return a:str
          > endif
          > return strpart(a:str,0,a:pos) . a:ch . strpart(a:str , a:pos + 1)
          > endfunction
          >
          > so I can do let s = Repl_in_str(s , 0 , 'Q')

          You can try:

          let s = 'qwerty'
          let pos = 3
          let ch = 'X'
          let s = substitute(s, '\%'.pos.'c.', ch, '')

          though I'm not sure it's a great improvement. (might differ in
          the indexing...zero-based vs. one-based)
          Just an idea.

          -tim




          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Maxim Kim
          ... let s= qwerty let s= Q .s[1:] --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use maillist. For more
          Message 4 of 5 , Apr 2, 2009
            On 2 апр, 19:34, Spiros Bousbouras <spi...@...> wrote:
            > let s = 'qwerty'
            > Let's say I want to replace the first character by Q. I thought
            > the following would work
            > let s[0] = 'Q'   but it gives
            > E689: Can only index a List or Dictionary
            > So what is the simplest way to achieve such a task ?
            ...
            > Anyone got a better idea ?

            let s='qwerty'
            let s='Q'.s[1:]

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Spiros Bousbouras
            ... That s much better. You can even do echo s[-3:-1] which gives rty. I wonder what is the purpose of the strpart() function when such a convenient notation
            Message 5 of 5 , Apr 2, 2009
              On 2 Apr, 17:14, Maxim Kim <haba...@...> wrote:
              > On 2 апр, 19:34, Spiros Bousbouras <spi...@...> wrote:
              >
              > > let s = 'qwerty'
              > > Let's say I want to replace the first character by Q. I thought
              > > the following would work
              > > let s[0] = 'Q' but it gives
              > > E689: Can only index a List or Dictionary
              > > So what is the simplest way to achieve such a task ?
              > ...
              > > Anyone got a better idea ?
              >
              > let s='qwerty'
              > let s='Q'.s[1:]

              That's much better. You can even do echo s[-3:-1] which
              gives rty. I wonder what is the purpose of the strpart()
              function when such a convenient notation exists.

              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            Your message has been successfully submitted and would be delivered to recipients shortly.