Skip to main content
trim(str, cutset) removes any character in cutset from the beginning and end of str.

Signature

trim(str string, cutset string) string
cutset is treated as a set of characters to remove, not a literal string to match. Every character in cutset is independently stripped.

Example

Strip unwanted characters from a version string that may have been tagged with v, V, or leading zeros:
variable "raw_version" {
  description = "Version string as read from an environment variable or tag"
  type        = string
}

computed "version" {
  description = "Cleaned version number"
  expression  = trim(var.raw_version, "vV0")
}

task "release" {
  description = "Create a release with the cleaned version"
  commands = [
    "gh release create ${computed.version} --generate-notes",
  ]
}
v01.2.3 becomes 1.2.3.

Notes

  • cutset is a set of characters, not a substring. trim("hello", "helo") returns an empty string because all those characters appear at the edges.
  • To remove a specific prefix or suffix string (not a set of characters), use trimprefix or trimsuffix.
  • To remove only whitespace, use trimspace.