Pythonic string.replace() in Nim?
Asked Answered
P

1

5

How to replace some string with Nim ?

var pythonista:string = "Power of Python!"
echo pythonista
Phenomenal answered 14/12, 2021 at 20:46 Comment(0)
P
7

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 "============"
Phenomenal answered 14/12, 2021 at 20:50 Comment(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 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/strutilsInimitable
@Yardanico, oh, I see. That was inattentive on my part. I'll fix my answer.Phenomenal

© 2022 - 2024 — McMap. All rights reserved.