Skip to content

You are viewing documentation for Instruqt 2.0 Labs - our upcoming product releasing in September 2026. For current Tracks documentation, please visitdocs.instruqt.com.

String Functions


String functions provide operations for text manipulation, formatting, and pattern matching in HCL expressions.

Removes newline characters at the end of a string.

chomp(string)

Parameters:

  • string - The string to process

Returns: String with trailing newlines removed

Example:

chomp("hello world\n") # Returns: "hello world"
chomp("hello\n\n") # Returns: "hello"
chomp("hello") # Returns: "hello"

Produces a string by formatting values according to a specification string.

format(spec, values...)

Parameters:

  • spec - Format specification string
  • values... - Values to format

Returns: Formatted string

Format Verbs:

  • %s - String
  • %d - Integer
  • %f - Float
  • %t - Boolean
  • %% - Literal percent sign

Example:

format("Hello %s", "world") # Returns: "Hello world"
format("Port %d", 8080) # Returns: "Port 8080"
format("%s-%02d", "web", 1) # Returns: "web-01"
format("%.2f%%", 99.9) # Returns: "99.90%"

Produces a list of strings by formatting according to a specification string.

formatlist(spec, values...)

Parameters:

  • spec - Format specification string
  • values... - Lists of values to format

Returns: List of formatted strings

Example:

formatlist("hello %s", ["alice", "bob", "charlie"])
# Returns: ["hello alice", "hello bob", "hello charlie"]
formatlist("%s-%d", ["web", "api"], [1, 2])
# Returns: ["web-1", "api-2"]

Adds spaces to the beginning of each line in a multi-line string.

indent(spaces, string)

Parameters:

  • spaces - Number of spaces to add
  • string - String to indent

Returns: Indented string

Example:

indent(2, "line1\nline2\nline3")
# Returns: "line1\n line2\n line3"
# Useful for formatting YAML in templates
indent(4, jsonencode({key = "value"}))

Concatenates list elements into a single string with a separator.

join(separator, list)

Parameters:

  • separator - String to place between elements
  • list - List of strings to join

Returns: Joined string

Example:

join(", ", ["apple", "banana", "cherry"]) # Returns: "apple, banana, cherry"
join("-", ["2024", "01", "15"]) # Returns: "2024-01-15"
join("", ["h", "e", "l", "l", "o"]) # Returns: "hello"

Converts all cased letters to lowercase.

lower(string)

Parameters:

  • string - String to convert

Returns: Lowercase string

Example:

lower("HELLO WORLD") # Returns: "hello world"
lower("MixedCase") # Returns: "mixedcase"
lower("already lower") # Returns: "already lower"

Applies a regular expression and returns the first match.

regex(pattern, string)

Parameters:

  • pattern - Regular expression pattern
  • string - String to search

Returns: First matching substring or error if no match

Example:

regex("[0-9]+", "abc123def") # Returns: "123"
regex("^[a-z]+", "hello123") # Returns: "hello"
regex("v([0-9.]+)", "version: v1.2.3") # Returns: "1.2.3"

Applies a regular expression and returns all matches.

regexall(pattern, string)

Parameters:

  • pattern - Regular expression pattern
  • string - String to search

Returns: List of all matching substrings

Example:

regexall("[0-9]+", "abc123def456") # Returns: ["123", "456"]
regexall("[a-z]+", "hello123world") # Returns: ["hello", "world"]
regexall(".", "abc") # Returns: ["a", "b", "c"]

Divides a string into a list at separator occurrences.

split(separator, string)

Parameters:

  • separator - String to split on
  • string - String to split

Returns: List of string parts

Example:

split(",", "apple,banana,cherry") # Returns: ["apple", "banana", "cherry"]
split(".", "192.168.1.1") # Returns: ["192", "168", "1", "1"]
split(" ", "hello world") # Returns: ["hello", "world"]

Reverses the characters in a string.

strrev(string)

Parameters:

  • string - String to reverse

Returns: Reversed string

Example:

strrev("hello") # Returns: "olleh"
strrev("12345") # Returns: "54321"
strrev("racecar") # Returns: "racecar"

Extracts a substring by position and length.

substr(string, offset, length)

Parameters:

  • string - String to extract from
  • offset - Starting position (0-based)
  • length - Maximum characters to extract

Returns: Extracted substring

Example:

substr("hello world", 0, 5) # Returns: "hello"
substr("hello world", 6, 5) # Returns: "world"
substr("hello world", 6, 100) # Returns: "world" (length exceeds string)

Capitalizes the first letter of each word.

title(string)

Parameters:

  • string - String to convert

Returns: Title-cased string

Example:

title("hello world") # Returns: "Hello World"
title("the quick brown fox") # Returns: "The Quick Brown Fox"
title("API_KEY") # Returns: "Api_key"

Removes whitespace from both ends of a string (custom hclconfig implementation).

trim(string)

Parameters:

  • string - String to trim

Returns: Trimmed string

Example:

trim(" hello ") # Returns: "hello"
trim("\n\thello\n\t") # Returns: "hello"
trim("hello") # Returns: "hello"

Removes a prefix from the start of a string.

trimprefix(string, prefix)

Parameters:

  • string - String to process
  • prefix - Prefix to remove

Returns: String with prefix removed (only if present)

Example:

trimprefix("helloworld", "hello") # Returns: "world"
trimprefix("helloworld", "world") # Returns: "helloworld"
trimprefix("/path/to/file", "/") # Returns: "path/to/file"

Removes a suffix from the end of a string.

trimsuffix(string, suffix)

Parameters:

  • string - String to process
  • suffix - Suffix to remove

Returns: String with suffix removed (only if present)

Example:

trimsuffix("helloworld", "world") # Returns: "hello"
trimsuffix("helloworld", "hello") # Returns: "helloworld"
trimsuffix("file.txt", ".txt") # Returns: "file"

Removes whitespace from both ends of a string.

trimspace(string)

Parameters:

  • string - String to trim

Returns: String with whitespace removed

Example:

trimspace(" hello ") # Returns: "hello"
trimspace("\n\thello\n\t") # Returns: "hello"
trimspace("hello") # Returns: "hello"

Converts all cased letters to uppercase.

upper(string)

Parameters:

  • string - String to convert

Returns: Uppercase string

Example:

upper("hello world") # Returns: "HELLO WORLD"
upper("MixedCase") # Returns: "MIXEDCASE"
upper("ALREADY UPPER") # Returns: "ALREADY UPPER"
local {
# Clean and normalize username
username = lower(trimspace(var.raw_username))
# Remove file extension
name_without_ext = trimsuffix(var.filename, ".txt")
}
resource "container" "web" {
count = 3
container_name = format("%s-web-%02d", var.environment, count.index + 1)
# Results: "prod-web-01", "prod-web-02", "prod-web-03"
}
local {
# Split comma-separated values
allowed_ips = split(",", var.ip_whitelist)
# Extract parts using regex
version_number = regex("v([0-9.]+)", var.image_tag)
}
resource "template" "config" {
source = format(<<-EOF
server {
listen %d;
server_name %s;
root %s;
}
EOF
, var.port, lower(var.domain), trimprefix(var.path, "/"))
destination = "./nginx.conf"
}