Skip to content
Stef Heyenrath edited this page Apr 29, 2024 · 19 revisions

Custom String Helpers

These helpers provide some ability to do string manipulation.


Append

Summary Append one string to another
Returns Appened string
Remarks
Parameters
str Base string
suffix String to append

Example

Context

{
    "a": "Lorem ipsum dolor sit amet",
    "b": ", consectetur adipiscing elit. Vivamus lacinia urna lectus."
}

Usage

<strong>result:</strong>
{{String.Append a b}}
{{String.Append a " or something"}}
{{String.Append "something or" " another"}}

Returns

<strong>result:</strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia urna lectus.
Lorem ipsum dolor sit amet or something
something or another

String.Base64Decode

Summary Base64 decode string
Returns Decoded string
Remarks
Parameters
str Base64 encoded string to decode

Example

Context

{
    "value": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gVml2YW11cyBsYWNpbmlhIHVybmEgbGVjdHVzLg=="
}

Usage

<strong>result:</strong>
{{String.Base64Decode value}}
{{String.Base64Decode "c29tZXRoaW5nIHdpY2tlZCB0aGlzIHdheSBjb21lcw=="}}

Returns

<strong>result:</strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia urna lectus.
something wicked this way comes

Base64Encode

Summary Base64 encode string
Returns Base64 encoded string
Remarks
Parameters
str String to Baes64 encode

Example

Context

{
    "value": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia urna lectus."
}

Usage

<strong>result:</strong>
{{String.Base64Encode value}}
{{String.Base64Encode "something wicked this way comes"}}

Returns

<strong>result:</strong>
TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gVml2YW11cyBsYWNpbmlhIHVybmEgbGVjdHVzLg==
c29tZXRoaW5nIHdpY2tlZCB0aGlzIHdheSBjb21lcw==

Ellipsis

Summary Truncate a long string and append a ellipsis(...)
Returns Truncated string
Remarks
Parameters
str String to truncate
limit Max number of characters in final string(excluding ellipsis)

Example

Context

{
    "value": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia urna lectus."
}

Usage

<strong>result:</strong>
{{String.Ellipsis value 10}}
{{String.Ellipsis "something" 5}}

Returns

<strong>result:</strong>
Lorem ipsu...
somet...

Join

Summary Join multiple strings together separated by the specified separator
Returns Joined string
Remarks Can be passed either an array of strings or a set of strings as arguments
Parameters
strArray strings to join
separator Separator to used to delimit strings, default is an empty string

Example

Context

{
    "stringArray": [
        "Lorem",
        "ipsum",
        "dolor",
        "sit",
        "amet"
    ]
}

Usage

<strong>result:</strong>
{{String.Join stringArray " "}}
{{String.Join stringArray}}
{{String.Join "Lorem" "ipsum" "dolor" "sit" "amet" " "}}
{{String.Join "Lorem" "ipsum" "dolor" "sit" "amet" ""}}
{{String.Join "Lorem" "ipsum" "dolor" "sit" "amet"}}

Returns

<strong>result:</strong>
Lorem ipsum dolor sit amet
Loremipsumdolorsitamet
Lorem ipsum dolor sit amet
Loremipsumdolorsitamet
Loremametipsumametdolorametsit

NOTE: The last call in the examples, if you pass in multiple strings instead of a string array, and there are more than two strings passed in, it treats the final string as the separator/delimiator. This means that if you tried {{String.Join "Lorem" "ipsum" "dolor" "sit" "amet"}} and wanted to get back Loremipsumdolorsitamet, then you would actually need to add one more string to your list; ex: {{String.Join "Lorem" "ipsum" "dolor" "sit" "amet" ""}}


Lowercase

Summary Convert string to lowercase
Returns Lowercase string
Remarks
Parameters
str input

Example

Context

{
    "value": "Some String WITH lots of UPPERCASE letters."
}

Usage

<strong>result:</strong>
{{String.Lowercase value}}
{{String.Lowercase "SOMETHING"}}

Returns

<strong>result:</strong>
some string with lots of uppercase letters.
something

Occurrences

Summary Count number of times a substring appears in base string
Returns Number of times string occurred
Remarks
Parameters
str Base string
substring String to match
ignoreCase Whether or not to ignore case, defaults to false

Example

Context

{
    "value": "Some string with multiple SubStrings.",
    "match": "string",
    "ignoreCase": true
}

Usage

<strong>result:</strong>
{{String.Occurrences value match}}
{{String.Occurrences value match ignoreCase}}
{{String.Occurrences value "string"}}
{{String.Occurrences value "string" true}}
{{String.Occurrences "something somewhere" "some"}}

Returns

<strong>result:</strong>
1
2
1
2
2

PadLeft

Summary Pad left input string with the padding character for a specified total length.
Returns Left padded string
Remarks
Parameters
str Base string
totalWidth The number of characters in the resulting string
paddingChar Padding character (not more than 1)

Example

Context

{
    "value": "pad-me"
}

Usage

<strong>result:</strong>
1. {{String.PadLeft value 10 "0"}}
2. {{String.PadLeft "custom-string" 15 "0"}}
3. {{String.PadLeft value 15 "more-than-1-chars"}}
4. {{String.PadLeft "custom-string" 15 "more-than-1-chars"}}

Returns

<strong>result:</strong>
1. 0000pad-me
2. 000custom-string
3. pad-me
4. custom-string

PadRight

Summary Pad right input string with the padding character for a specified total length.
Returns Right padded string
Remarks
Parameters
str Base string
totalWidth The number of characters in the resulting string
paddingChar Padding character (not more than 1)

Example

Context

{
    "value": "pad-me"
}

Usage

<strong>result:</strong>
1. {{String.PadRight value 10 "0"}}
2. {{String.PadRight "custom-string" 15 "0"}}
3. {{String.PadRight value 15 "more-than-1-chars"}}
4. {{String.PadRight "custom-string" 15 "more-than-1-chars"}}

Returns

<strong>result:</strong>
1. pad-me0000
2. custom-string000
3. pad-me
4. custom-string

Prepend

Summary Prepend one string to another
Returns Prepend string
Remarks
Parameters
str Base string
prefix String to prepend

Example

Context

{
    "a": "Lorem ipsum dolor sit amet",
    "b": ", consectetur adipiscing elit. Vivamus lacinia urna lectus."
}

Usage

<strong>result:</strong>
{{String.Prepend b a}}
{{String.Prepend a "something or "}}
{{String.Prepend "another" "something or "}}

Returns

<strong>result:</strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia urna lectus.
something or Lorem ipsum dolor sit amet
something or another

Replace

Summary Replace matching substrings within a string with something else.
Returns Replaced string
Remarks Not case-insensitive
Parameters
str Base string
a String to match
b Replacement

Example

Context

{
    "value": "Some string with multiple Substrings.",
    "match": "string",
    "replacement": "value"
}

Usage

<strong>result:</strong>
{{String.Replace value match replacement}}
{{String.Replace value match "test"}}
{{String.Replace value "string" "test"}}
{{String.Replace value "string" replacement}}
{{String.Replace "something somewhere" "some" "test"}}

Returns

<strong>result:</strong>
Some value with multiple Subvalues.
Some test with multiple Subtests.
Some test with multiple Subtests.
Some value with multiple Subvalues.
testthing testwhere

Reverse

Summary Reverse a string
Returns Reversed string
Remarks
Parameters
str Input

Example

Context

{
    "value": "Some String WITH lots of UPPERCASE letters."
}

Usage

<strong>result:</strong>
{{String.Reverse value}}
{{String.Reverse "SOMETHING"}}

Returns

<strong>result:</strong>
.srettel ESACREPPU fo stol HTIW gnirtS emoS
GNIHTEMOS

Split

Summary Split a string
Returns array
Remarks
Parameters
str input
ch split character

Example

Usage

{{#each (String.Split \"a;b;c\" ';')}}\r\n{{@Key}}:{{@Index}}:{{this}}\r\n{{/each}}

Returns

0:0:a
1:1:b
2:2:c

Substring

Summary Retrieves a substring from the original string
Returns Substring portion
Remarks Behavior of parameters follows String.Substring() behavior
Parameters
str Base string
start Start position of the substring
length Length of the string (optional)

Example

Context

{
    "value": "Lorem ipsum dolor sit amet"
}

Usage

<strong>result:</strong>
{{String.Substring value 1 3}}
{{String.Substring "Lorem" 1 }}

Returns

<strong>result:</strong>
ore
orem

Titlecase

Summary Convert string to titlecase
Returns Titlecase string
Remarks
Parameters
str input

Example

Context

{
    "value": "Some String WITH lots of UPPERCASE letters."
}

Usage

<strong>result:</strong>
{{String.Titlecase value}}
{{String.Titlecase "SOMETHING"}}

Returns

<strong>result:</strong>
Some String With Lots Of Uppercase Letters.
Something

Trim

Summary Trim whitespace from left and right side of string
Returns Trimmed string
Remarks
Parameters
str Base string

Example

Context

{
    "value": "      Some String WITH lots of UPPERCASE letters.      "
}

Usage

<strong>result:</strong>
{{String.Trim value}}
{{String.Trim "      SOMETHING      "}}

Returns

<strong>result:</strong>
Some String WITH lots of UPPERCASE letters.
SOMETHING

TrimLeft

Summary Trim whitespace from left side of string
Returns Trimmed string
Remarks
Parameters
str Base string

Example

Context

{
    "value": "      Some String WITH lots of UPPERCASE letters.      "
}

Usage

<strong>result:</strong>
{{String.TrimLeft value}}
{{String.TrimLeft "      SOMETHING      "}}

Returns

<strong>result:</strong>
Some String WITH lots of UPPERCASE letters.      
SOMETHING      

TrimRight

Summary Trim whitespace from right side of string
Returns Trimmed string
Remarks
Parameters
str Base string

Example

Context

{
    "value": "      Some String WITH lots of UPPERCASE letters.      "
}

Usage

<strong>result:</strong>
{{String.TrimRight value}}
{{String.TrimRight "      SOMETHING      "}}

Returns

<strong>result:</strong>
      Some String WITH lots of UPPERCASE letters.
      SOMETHING

Truncate

Summary Truncate a long string
Returns Truncated string
Remarks
Parameters
str String to truncate
limit Max number of characters in final string

Example

Context

{
    "value": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia urna lectus."
}

Usage

<strong>result:</strong>
{{String.Truncate value 10}}
{{String.Truncate "something" 5}}

Returns

<strong>result:</strong>
Lorem ipsu
somet

TruncateWords

Summary Truncate a long string and base truncation on the number of words instead of characters and append a suffix(if set)
Returns Truncated string
Remarks
Parameters
str String to trucate
limit Number of words in final string(excluding suffix)
suffix Suffix to append if string gets trucated

Example

Context

{
    "value": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia urna lectus."
}

Usage

<strong>result:</strong>
{{String.TruncateWords value 10}}
{{String.TruncateWords value 10 "..."}}
{{String.TruncateWords "something wicked this way comes" 2}}
{{String.TruncateWords "something wicked this way comes" 2 "test"}}

Returns

<strong>result:</strong>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia...
something wicked
something wickedtest

Uppercase

Summary Convert string to uppercase
Returns Uppercase string
Remarks
Parameters
str input

Example

Context

{
    "value": "Some String WITH lots of lowercase letters."
}

Usage

<strong>result:</strong>
{{String.Uppercase value}}
{{String.Uppercase "something"}}

Returns

<strong>result:</strong>
SOME STRING WITH LOTS OF LOWERCASE LETTERS.
SOMETHING

Format

Summary Format a DateTime as a string
Returns string
Remarks
Parameters
format The DateTime format to use

Example

Context

var model = new 
{
  x = DateTime.Now
};

Usage

<strong>result:</strong>
{{String.Format x \"yyyy-MMM-dd\"}}
{{String.Format (Now) \"yyyy-MMM-dd\"}}

Returns

<strong>result:</strong>
2020-Jan-27
2020-Jan-27
Clone this wiki locally