String Functions
String functions allow manipulating, normalizing, trimming, and formatting character strings in locals.yml expressions and variables.yml validation conditions.
Function Index
Section titled “Function Index”| Function | Signature | Description |
|---|---|---|
lower |
lower(string) |
Converts all characters to lowercase. |
upper |
upper(string) |
Converts all characters to uppercase. |
trim |
trim(string, [cutset]) |
Removes leading and trailing whitespace or specified characters. |
trimprefix |
trimprefix(string, prefix) |
Removes the specified prefix if present. |
trimsuffix |
trimsuffix(string, suffix) |
Removes the specified suffix if present. |
startswith |
startswith(string, prefix) |
Returns true if the string starts with prefix. |
endswith |
endswith(string, suffix) |
Returns true if the string ends with suffix. |
replace |
replace(string, search, rep) |
Replaces all occurrences of a search pattern. |
split |
split(separator, string) |
Splits a string into a list by separator. |
join |
join(separator, list) |
Joins list elements into a single string. |
format |
format(format, ...args) |
Formats a string using printf-style verbs (%s, %d, %j, %%). |
substr |
substr(string, offset, len) |
Extracts a substring from an offset. |
Converts all characters in a string to lowercase.
testform functions --eval "lower('STAGING-APP')"# => "staging-app"locals: canonical_env: "${lower(var.environment)}"Converts all characters in a string to uppercase.
testform functions --eval "upper('qa_suite')"# => "QA_SUITE"Removes leading and trailing whitespace. Optionally accepts a second argument specifying the character set to remove.
testform functions --eval "trim(' hello world ')"# => "hello world"
testform functions --eval "trim('---alert---', '-')"# => "alert"trimprefix
Section titled “trimprefix”Removes the specified prefix from the beginning of the string if present.
testform functions --eval "trimprefix('https://api.example.com', 'https://')"# => "api.example.com"trimsuffix
Section titled “trimsuffix”Removes the specified suffix from the end of the string if present.
testform functions --eval "trimsuffix('tests/runs/suite.run.feature', '.run.feature')"# => "tests/runs/suite"startswith
Section titled “startswith”Returns true if the string begins with the indicated prefix. Useful for input variable validation.
variable: api_endpoint: type: string validation: condition: "${startswith(var.api_endpoint, 'https://')}" error_message: "API URL must use the secure HTTPS protocol."endswith
Section titled “endswith”Returns true if the string ends with the indicated suffix.
variable: spec_file: type: string validation: condition: "${endswith(var.spec_file, '.case.feature')}" error_message: "Specification file must end with .case.feature."replace
Section titled “replace”Replaces all occurrences of a search pattern with a replacement string.
testform functions --eval "replace('192.168.1.1', '.', '-')"# => "192-168-1-1"Divides a string into a list of strings using the specified delimiter.
testform functions --eval "split(',', 'smoke,regression,e2e')"# => ["smoke", "regression", "e2e"]Concatenates elements of a string list using a delimiter.
testform functions --eval "join(' / ', ['Checkout', 'Payments', 'Card'])"# => "Checkout / Payments / Card"format
Section titled “format”Formats a string using C / Terraform-style format specifiers:
%s: String substitution.%d: Integer substitution.%j: Serialized JSON substitution.%%: Literal%escape.
testform functions --eval "format('https://%s.example.com:%d/v1', 'api', 8080)"# => "https://api.example.com:8080/v1"locals: service_url: "${format('https://%s.%s', var.subdomain, var.domain)}"substr
Section titled “substr”Extracts a substring of length len starting at offset. If offset is negative, it counts backwards from the end of the string.
testform functions --eval "substr('20261025_release', 0, 8)"# => "20261025"
testform functions --eval "substr('file.json', -4, 4)"# => "json"