How do I join or concatenate strings in EK9?

← Common String Operations · Ref: Q174

EK9 provides several ways to combine strings: backtick interpolation (preferred), the + operator (for simple cases), += append, and stream collect.

BACKTICK INTERPOLATION (preferred)
Embed expressions in backtick strings:

  stdout.println(`Welcome ${name}, you are ${age} years old`)

Any expression inside ${...} is evaluated and converted to string. Interpolation compiles to a single allocation via a bespoke STRING_INTERPOLATION IR instruction. Always prefer this for 3+ parts.

PLUS OPERATOR (2-part only)
Concatenate two strings with +:

  full <- first + second

Creates a new string from two parts. Chains of 3 or more parts (e.g. a + b + c) trigger compiler error E11068 because each + creates an intermediate String object. Use interpolation instead:

  BAD:  result <- first + " " + second
  GOOD: result <- `${first} ${second}`

APPEND (+=)

Mutate a string variable by appending:

  greeting <- "Hello"
  greeting += " World"

STREAM COLLECT

Join a list of strings via stream pipeline:

  combined <- cat ["Hello", " ", "World"] | collect as String

See Q37 for string basics. See Q43 for escape sequences in interpolation. See Q309 for the E11068 compiler error details.

Example

defines module qa.stringops.concat

  defines program
    StringConcatDemo()
      stdout <- Stdout()

      first <- "Hello"
      second <- "World"

      // === BACKTICK INTERPOLATION (preferred) ===

      // Interpolation compiles to a single allocation
      combined <- `${first} ${second}`
      stdout.println(`Interpolated: ${combined}`)

      name <- "Steve"
      age <- 30
      stdout.println(`Name: ${name}, Age: ${age}`)

      // Expressions inside interpolation
      stdout.println(`Upper: ${name.upperCase()}`)
      stdout.println(`Length: ${length name}`)

      // === PLUS OPERATOR (2-part only) ===

      // Two-part concatenation is allowed
      twoPartConcat <- first + second
      stdout.println(`Two-part: ${twoPartConcat}`)

      // Three or more parts would trigger E11068:
      //   result <- first + " " + second   // Error!
      // Use interpolation instead:
      //   result <- `${first} ${second}`    // Correct

      // === APPEND (+=) ===

      greeting <- "Hello"
      greeting += " "
      greeting += "World"
      stdout.println(`Appended: ${greeting}`)

      // === STREAM COLLECT ===

      parts <- ["Hello", " ", "World", "!"]
      joined <- cat parts | collect as String
      stdout.println(`Stream joined: ${joined}`)

      // === BUILDING FROM MULTIPLE VALUES ===

      host <- "localhost"
      port <- 8080
      url <- `http://${host}:${port}`
      stdout.println(`URL: ${url}`)

      // Interpolation is cleaner for mixed types
      stdout.println(`URL: http://${host}:${port}`)

Common mistakes

E11068 — Concatenating 3 or more string parts with + triggers E11068. Use backtick interpolation instead. See ek9 -h E11068 for details.

Incorrect:

combined <- first + " " + second

Correct:

combined <- `${first} ${second}`
Other ways to ask this
  • How do I combine strings together in EK9?
  • What is the EK9 equivalent of string concatenation?
  • How do I embed expressions inside EK9 strings?

Coming from another language?

Java: + operator, String.concat(), StringBuilder, String.format(). Python: + operator, f-strings, .join(). Rust: format!() macro, + operator, .push_str(). Go: + operator, fmt.Sprintf(). JavaScript: + operator, template literals. Kotlin: + operator, string templates. EK9: backtick interpolation (preferred), + operator (2-part only, 3+ is E11068), += append, stream collect.

Keywords: append, interpolation, E11068, combine, string, plus, text, join, concat, merge, concatenate