• HashiCorp Developer

  • HashiCorp Cloud Platform
  • Terraform
  • Packer
  • Consul
  • Vault
  • Boundary
  • Nomad
  • Waypoint
  • Vagrant
Terraform
  • Install
  • Tutorials
    • About the Docs
    • Configuration Language
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
    • CDK for Terraform
    • Provider Use
    • Plugin Development
    • Registry Publishing
    • Integration Program
  • Registry(opens in new tab)
  • Try Cloud(opens in new tab)
  • Sign up
Terraform Home

Configuration Language

Skip to main content
  • Terraform Language

  • Overview
  • Data Sources
    • Overview
      • chomp
      • endswith
      • format
      • formatlist
      • indent
      • join
      • lower
      • regex
      • regexall
      • replace
      • split
      • startswith
      • strrev
      • substr
      • title
      • trim
      • trimprefix
      • trimsuffix
      • trimspace
      • upper
  • Upgrading to Terraform v1.3
  • v1.x Compatibility Promises

  • Terraform Internals

  • Resources

  • Tutorial Library
  • Certifications
  • Community Forum
    (opens in new tab)
  • Support
    (opens in new tab)
  • GitHub
    (opens in new tab)
  • Terraform Registry
    (opens in new tab)
  1. Developer
  2. Terraform
  3. Configuration Language
  4. Functions
  5. substr
  • Terraform
  • v1.2.x
  • v1.1 and earlier

»substr Function

substr extracts a substring from a given string by offset and (maximum) length.

substr(string, offset, length)

Examples

> substr("hello world", 1, 4)
ello

The offset and length are both counted in unicode characters rather than bytes:

> substr("🤔🤷", 0, 1)
🤔

The offset index may be negative, in which case it is relative to the end of the given string. The length may be -1, in which case the remainder of the string after the given offset will be returned.

> substr("hello world", -5, -1)
world

If the length is greater than the length of the string, the substring will be the length of all remaining characters.

> substr("hello world", 6, 10)
world
Edit this page on GitHub

On this page

  1. substr Function
  2. Examples
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)