How do you do string interpolation with variables in nim?

谁说我不能喝 提交于 2020-08-20 11:18:22

问题


I've seen that you can interpolate strings with fmt like so:

    let msg = "hello"
    echo fmt"{msg}\n"

But in my case, the interpolated string is quite long. I would prefer to assign said text to a variable and then the the interpolation later, like so:

let msg = "..... long text here {place_holder1}...."
echo interpolate(msg, var1, etc)

Is this possible?


回答1:


Yes, see strutils.format.

strutils also comes with the % operator which can be used like:

let str = "$#, $#, $#"
let interp = str % ["One", "Two", "Three"]
echo(interp) # echos One, Two, Three


来源:https://stackoverflow.com/questions/58739226/how-do-you-do-string-interpolation-with-variables-in-nim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!