Nim: Concatenating strings

Mar 21, 2022

Concatenating strings is one of those topics where for any given language there's a nearly infinite way to achieve the end result. Though typically there will be one or two approaches that are deemed more idiomatic. Here's a couple of the more common approaches within Nim

var a = "This"
a.add(" is a string")            
echo "a is now: ", a
# a is now: This is a string

  b = "This"
  c " is a string"
echo "concat: ", b & c
# concat: This is a string

You can also import strutils to use some functions that will look familiar from other languages:

# calling as a method on an array:
import strutils
echo @["a", "b", "c"].join(" ")
# a b c

# or as a function:
echo join(@["a", "b", "c"], "xx")
# axxbxxc
Hi, I'm Glenn! 👋 I'm currently Director of Product (Terraform) @ HashiCorp, and we're hiring! If you'd like to come and work with me and help make Terraform Cloud even more amazing we have multiple positions opening in Product ManagementDesign, and Engineering & Engineering Management across a range of levels (i.e., junior through to senior). Please send in an application ASAP so we can get in touch.