From fa8a87fab58166de33b7ceeb63bb906bc20bfbb2 Mon Sep 17 00:00:00 2001 From: Ben Partridge Date: Mon, 7 Feb 2022 15:12:22 +1100 Subject: [PATCH] Adds documentation to `substr` function to cover when `length` longer than input `string` The `substr` function allows the `length` parameter to be longer than the remaining characters in the input after the offset. This is useful for when you want to truncate a string to a maximum number of characters. However, the documentation isn't clear on this so I had to do a test deployment to confirm the behaviour after finding the behaviour in an old issue https://github.com/hashicorp/terraform/issues/15751 --- website/docs/language/functions/substr.mdx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/website/docs/language/functions/substr.mdx b/website/docs/language/functions/substr.mdx index dab4e3a8f..1ccd24cc1 100644 --- a/website/docs/language/functions/substr.mdx +++ b/website/docs/language/functions/substr.mdx @@ -7,7 +7,7 @@ description: |- # `substr` Function -`substr` extracts a substring from a given string by offset and length. +`substr` extracts a substring from a given string by offset and (maximum) length. ```hcl substr(string, offset, length) @@ -36,3 +36,11 @@ string after the given offset will be returned. > substr("hello world", -5, -1) world ``` + +The length may be greater than the length of the string, in which case the substring +will be the length of all remaining characters. + +``` +> substr("hello world", 6, 10) +world +```