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
-~----------~----~----~----~------~----~------~--~---

Reply via email to