5

How to replace some string with Nim ?

var pythonista:string = "Power of Python!"
echo pythonista
How2
  • 365
  • 1
  • 6

1 Answers1

8

First, you need to import strutils module. Strutils is the module that defines several common functions for working with strings.

So the code will be like this:

import std/strutils
    
var pythonista:string = "Power of Python!"
echo pythonista
echo "============"
echo pythonista.replace("Python!", "Nim :)")
echo "============"
eglease
  • 2,445
  • 11
  • 18
  • 28
How2
  • 365
  • 1
  • 6
  • 2
    One thing I'd want to clarify to prevent confusion is that strutils is not a "system" module. You can call it a standard library module, but not system, as https://nim-lang.org/docs/system.html is the "main" module that's always implicitly imported in all Nim files, and it's not related to strutils. Another small note - it's recommended to import stdlib modules with the `std` namespace prefix, so `import std/strutils` – Optimon Dec 14 '21 at 20:51
  • @Yardanico, oh, I see. That was inattentive on my part. I'll fix my answer. – How2 Dec 14 '21 at 20:59