From fa8a87fab58166de33b7ceeb63bb906bc20bfbb2 Mon Sep 17 00:00:00 2001 From: Ben Partridge Date: Mon, 7 Feb 2022 15:12:22 +1100 Subject: [PATCH 1/2] 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 dab4e3a8f92b..1ccd24cc1d48 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 +``` From 252865c6a034301f521565d14518e73be39ff650 Mon Sep 17 00:00:00 2001 From: Ben Partridge Date: Tue, 15 Feb 2022 10:23:37 +1100 Subject: [PATCH 2/2] Improvements to wording of substr function documentation Co-authored-by: Laura Pacilio <83350965+laurapacilio@users.noreply.github.com> --- website/docs/language/functions/substr.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/language/functions/substr.mdx b/website/docs/language/functions/substr.mdx index 1ccd24cc1d48..8317848de791 100644 --- a/website/docs/language/functions/substr.mdx +++ b/website/docs/language/functions/substr.mdx @@ -37,7 +37,7 @@ string after the given offset will be returned. world ``` -The length may be greater than the length of the string, in which case the substring +If the length is greater than the length of the string, the substring will be the length of all remaining characters. ```