String functions | Scriban
- Home
- Docs
- Built-in functions
- String
String functions available through the builtin object 'string`.
string.escapestring.appendstring.capitalizestring.capitalizewordsstring.containsstring.emptystring.whitespacestring.downcasestring.ends_withstring.equals_ignore_casestring.handleizestring.literalstring.lstripstring.pluralizestring.prependstring.removestring.remove_firststring.remove_laststring.replacestring.replace_firststring.rstripstring.sizestring.slicestring.slice1string.splitstring.starts_withstring.stripstring.strip_newlinesstring.to_intstring.to_longstring.to_floatstring.to_doublestring.truncatestring.truncatewordsstring.upcasestring.md5string.sha1string.sha256string.sha512string.hmac_sha1string.hmac_sha256string.hmac_sha512string.pad_leftstring.pad_rightstring.base64_encodestring.base64_decodestring.index_of
string.escape
string.escape <text>
Description
Escapes a string with escape characters.
Arguments
text: The input string
Returns
The two strings concatenated
Examples
input Try out
{{ "Hel\tlo\n\"W\\orld" | string.escape }}
output
Hel\tlo\n\"W\\orld
string.append
string.append <text> <with>
Description
Concatenates two strings
Arguments
text: The input stringwith: The text to append
Returns
The two strings concatenated
Examples
input Try out
{{ "Hello" | string.append " World" }}
output
Hello World
string.capitalize
string.capitalize <text>
Description
Converts the first character of the passed string to a upper case character.
Arguments
text: The input string
Returns
The capitalized input string
Examples
input Try out
{{ "test" | string.capitalize }}
output
Test
string.capitalizewords
string.capitalizewords <text>
Description
Converts the first character of each word in the passed string to a upper case character.
Arguments
text: The input string
Returns
The capitalized input string
Examples
input Try out
{{ "This is easy" | string.capitalizewords }}
output
This Is Easy
string.contains
string.contains <text> <value>
Description
Returns a boolean indicating whether the input string contains the specified string value.
Arguments
text: The input stringvalue: The string to look for
Returns
if text contains the string value
Examples
input Try out
{{ "This is easy" | string.contains "easy" }}
output
true
string.empty
string.empty <text>
Description
Returns a boolean indicating whether the input string is an empty string.
Arguments
text: The input string
Returns
if text is an empty string
Examples
input Try out
{{ "" | string.empty }}
output
true
string.whitespace
string.whitespace <text>
Description
Returns a boolean indicating whether the input string is empty or contains only whitespace characters.
Arguments
text: The input string
Returns
if text is empty string or contains only whitespace characters
Examples
input Try out
{{ "" | string.whitespace }}
output
true
string.downcase
string.downcase <text>
Description
Converts the string to lower case.
Arguments
text: The input string
Returns
The input string lower case
Examples
input Try out
{{ "TeSt" | string.downcase }}
output
test
string.ends_with
string.ends_with <text> <value>
Description
Returns a boolean indicating whether the input string ends with the specified string value.
Arguments
text: The input stringvalue: The string to look for
Returns
if text ends with the specified string value
Examples
input Try out
{{ "This is easy" | string.ends_with "easy" }}
output
true
string.equals_ignore_case
string.equals_ignore_case <text> <value>
Description
Returns a boolean indicating whether the input string is equal to specified string 'value'. Comparison is case insensitive.
Arguments
text: The input stringvalue: The string to compare
Returns
if text is equal to string value, ignoring case
Examples
input Try out
{{ "Scriban" | string.equals_ignore_case "SCRIBAN" }}
output
true
string.handleize
string.handleize <text>
Description
Returns a url handle from the input string.
Arguments
text: The input string
Returns
A url handle
Examples
input Try out
{{ '100% M & Ms!!!' | string.handleize }}
output
100-m-ms
string.literal
string.literal <text>
Description
Return a string literal enclosed with double quotes of the input string.
Arguments
text: The string to return a literal from.
Returns
The literal of a string.
Examples
If the input string has non printable characters or they need contain a double quote, they will be escaped.
input Try out
{{ 'Hello\n"World"' | string.literal }}
output
"Hello\n\"World\""
string.lstrip
string.lstrip <text>
Description
Removes any whitespace characters on the left side of the input string.
Arguments
text: The input string
Returns
The input string without any left whitespace characters
Examples
input Try out
{{ ' too many spaces' | string.lstrip }}
Highlight to see the empty spaces to the right of the string output
too many spaces
string.pluralize
string.pluralize <number> <singular> <plural>
Description
Outputs the singular or plural version of a string based on the value of a number.
Arguments
number: The number to checksingular: The singular string to return if number is == 1plural: The plural string to return if number is != 1
Returns
The singular or plural string based on number
Examples
input Try out
{{ products.size }} {{products.size | string.pluralize 'product' 'products' }}
output
7 products
string.prepend
string.prepend <text> <by>
Description
Concatenates two strings by placing the by string in from of the text string
Arguments
text: The input stringby: The string to prepend totext
Returns
The two strings concatenated
Examples
input Try out
{{ "World" | string.prepend "Hello " }}
output
Hello World
string.remove
string.remove <text> <remove>
Description
Removes all occurrences of a substring from a string.
Arguments
text: The input stringremove: The substring to remove from thetextstring
Returns
The input string with the all occurence of a substring removed
Examples
input Try out
{{ "Hello, world. Goodbye, world." | string.remove "world" }}
output
Hello, . Goodbye, .
string.remove_first
string.remove_first <text> <remove>
Description
Removes the first occurrence of a substring from a string.
Arguments
text: The input stringremove: The first occurence of substring to remove from thetextstring
Returns
The input string with the first occurence of a substring removed
Examples
input Try out
{{ "Hello, world. Goodbye, world." | string.remove_first "world" }}
output
Hello, . Goodbye, world.
string.remove_last
string.remove_last <text> <remove>
Description
Removes the last occurrence of a substring from a string.
Arguments
text: The input stringremove: The last occurence of substring to remove from thetextstring
Returns
The input string with the first occurence of a substring removed
Examples
input Try out
{{ "Hello, world. Goodbye, world." | string.remove_last "world" }}
output
Hello, world. Goodbye, .
string.replace
string.replace <text> <match> <replace>
Description
Replaces all occurrences of a string with a substring.
Arguments
text: The input stringmatch: The substring to find in thetextstringreplace: The substring used to replace the string matched bymatchin the inputtext
Returns
The input string replaced
Examples
input Try out
{{ "Hello, world. Goodbye, world." | string.replace "world" "buddy" }}
output
Hello, buddy. Goodbye, buddy.
string.replace_first
string.replace_first <text> <match> <replace> <fromEnd: False>?
Description
Replaces the first occurrence of a string with a substring.
Arguments
text: The input stringmatch: The substring to find in thetextstringreplace: The substring used to replace the string matched bymatchin the inputtextfromEnd: if true start match from end
Returns
The input string replaced
Examples
input Try out
{{ "Hello, world. Goodbye, world." | string.replace_first "world" "buddy" }}
output
Hello, buddy. Goodbye, world.
string.rstrip
string.rstrip <text>
Description
Removes any whitespace characters on the right side of the input string.
Arguments
text: The input string
Returns
The input string without any left whitespace characters
Examples
input Try out
{{ ' too many spaces ' | string.rstrip }}
Highlight to see the empty spaces to the right of the string output
too many spaces
string.size
string.size <text>
Description
Returns the number of characters from the input string
Arguments
text: The input string
Returns
The length of the input string
Examples
input Try out
{{ "test" | string.size }}
output
4
string.slice
string.slice <text> <start> <length>?
Description
The slice returns a substring, starting at the specified index. An optional second parameter can be passed to specify the length of the substring. If no second parameter is given, a substring with the remaining characters will be returned.
Arguments
text: The input stringstart: The starting index character where the slice should start from the inputtextstringlength: The number of character. Default is 0, meaning that the remaining of the string will be returned.
Returns
The input string sliced
Examples
input Try out
{{ "hello" | string.slice 0 }}
{{ "hello" | string.slice 1 }}
{{ "hello" | string.slice 1 3 }}
{{ "hello" | string.slice 1 length:3 }}
output
hello
ello
ell
ell
string.slice1
string.slice1 <text> <start> <length: 1>?
Description
The slice returns a substring, starting at the specified index. An optional second parameter can be passed to specify the length of the substring. If no second parameter is given, a substring with the first character will be returned.
Arguments
text: The input stringstart: The starting index character where the slice should start from the inputtextstringlength: The number of character. Default is 1, meaning that only the first character atstartposition will be returned.
Returns
The input string sliced
Examples
input Try out
{{ "hello" | string.slice1 0 }}
{{ "hello" | string.slice1 1 }}
{{ "hello" | string.slice1 1 3 }}
{{ "hello" | string.slice1 1 length: 3 }}
output
h
e
ell
ell
string.split
string.split <text> <match>
Description
The split function takes on a substring as a parameter.
The substring is used as a delimiter to divide a string into an array. You can output different parts of an array using array functions.
Arguments
text: The input stringmatch: The string used to split the inputtextstring
Returns
An enumeration of the substrings
Examples
input Try out
{{ for word in "Hi, how are you today?" | string.split ' ' ~}}
{{ word }}
{{ end ~}}
output
Hi,
how
are
you
today?
string.starts_with
string.starts_with <text> <value>
Description
Returns a boolean indicating whether the input string starts with the specified string value.
Arguments
text: The input stringvalue: The string to look for
Returns
if text starts with the specified string value
Examples
input Try out
{{ "This is easy" | string.starts_with "This" }}
output
true
string.strip
string.strip <text>
Description
Removes any whitespace characters on the left and right side of the input string.
Arguments
text: The input string
Returns
The input string without any left and right whitespace characters
Examples
input Try out
{{ ' too many spaces ' | string.strip }}
Highlight to see the empty spaces to the right of the string output
too many spaces
string.strip_newlines
string.strip_newlines <text>
Description
Removes any line breaks/newlines from a string.
Arguments
text: The input string
Returns
The input string without any breaks/newlines characters
Examples
input Try out
{{ "This is a string.\r\n With \nanother \rstring" | string.strip_newlines }}
output
This is a string. With another string
string.to_int
string.to_int <text>
Description
Converts a string to an integer
Arguments
text: The input string
Returns
A 32 bit integer or null if conversion failed
Examples
input Try out
{{ "123" | string.to_int + 1 }}
output
124
string.to_long
string.to_long <text>
Description
Converts a string to a long 64 bit integer
Arguments
text: The input string
Returns
A 64 bit integer or null if conversion failed
Examples
input Try out
{{ "123678912345678" | string.to_long + 1 }}
output
123678912345679
string.to_float
string.to_float <text>
Description
Converts a string to a float
Arguments
text: The input string
Returns
A 32 bit float or null if conversion failed
Examples
input Try out
{{ "123.4" | string.to_float + 1 }}
output
124.4
string.to_double
string.to_double <text>
Description
Converts a string to a double
Arguments
text: The input string
Returns
A 64 bit float or null if conversion failed
Examples
input Try out
{{ "123.4" | string.to_double + 1 }}
output
124.4
string.truncate
string.truncate <text> <length> <ellipsis>?
Description
Truncates a string down to the number of characters passed as the first parameter. An ellipsis (...) is appended to the truncated string and is included in the character count
Arguments
text: The input stringlength: The maximum length of the output string, including the length of theellipsisellipsis: The ellipsis to append to the end of the truncated string
Returns
The truncated input string
Examples
input Try out
{{ "The cat came back the very next day" | string.truncate 13 }}
output
The cat ca...
string.truncatewords
string.truncatewords <text> <count> <ellipsis>?
Description
Truncates a string down to the number of words passed as the first parameter. An ellipsis (...) is appended to the truncated string.
Arguments
text: The input stringcount: The number of words to keep from the inputtextstring before appending theellipsisellipsis: The ellipsis to append to the end of the truncated string
Returns
The truncated input string
Examples
input Try out
{{ "The cat came back the very next day" | string.truncatewords 4 }}
output
The cat came back...
string.upcase
string.upcase <text>
Description
Converts the string to uppercase
Arguments
text: The input string
Returns
The input string upper case
Examples
input Try out
{{ "test" | string.upcase }}
output
TEST
string.md5
string.md5 <text>
Description
Computes the md5 hash of the input string
Arguments
text: The input string
Returns
The md5 hash of the input string
Examples
input Try out
{{ "test" | string.md5 }}
output
098f6bcd4621d373cade4e832627b4f6
string.sha1
string.sha1 <text>
Description
Computes the sha1 hash of the input string
Arguments
text: The input string
Returns
The sha1 hash of the input string
Examples
input Try out
{{ "test" | string.sha1 }}
output
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
string.sha256
string.sha256 <text>
Description
Computes the sha256 hash of the input string
Arguments
text: The input string
Returns
The sha256 hash of the input string
Examples
input Try out
{{ "test" | string.sha256 }}
output
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
string.sha512
string.sha512 <text>
Description
Computes the sha512 hash of the input string
Arguments
text: The input string
Returns
The sha512 hash of the input string
Examples
input Try out
{{ "test" | string.sha512 }}
output
ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff
string.hmac_sha1
string.hmac_sha1 <text> <secretKey>
Description
Converts a string into a SHA-1 hash using a hash message authentication code (HMAC). Pass the secret key for the message as a parameter to the function.
Arguments
text: The input stringsecretKey: The secret key
Returns
The SHA-1 hash of the input string using a hash message authentication code (HMAC)
Examples
input Try out
{{ "test" | string.hmac_sha1 "secret" }}
output
1aa349585ed7ecbd3b9c486a30067e395ca4b356
string.hmac_sha256
string.hmac_sha256 <text> <secretKey>
Description
Converts a string into a SHA-256 hash using a hash message authentication code (HMAC). Pass the secret key for the message as a parameter to the function.
Arguments
text: The input stringsecretKey: The secret key
Returns
The SHA-256 hash of the input string using a hash message authentication code (HMAC)
Examples
input Try out
{{ "test" | string.hmac_sha256 "secret" }}
output
0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914
string.hmac_sha512
string.hmac_sha512 <text> <secretKey>
Description
Converts a string into a SHA-512 hash using a hash message authentication code (HMAC). Pass the secret key for the message as a parameter to the function.
Arguments
text: The input stringsecretKey: The secret key
Returns
The SHA-512 hash of the input string using a hash message authentication code (HMAC)
Examples
input Try out
{{ "test" | string.hmac_sha512 "secret" }}
output
f8a4f0a209167bc192a1bffaa01ecdb09e06c57f96530d92ec9ccea0090d290e55071306d6b654f26ae0c8721f7e48a2d7130b881151f2cec8d61d941a6be88a
string.pad_left
string.pad_left <text> <width>
Description
Pads a string with leading spaces to a specified total length.
Arguments
text: The input stringwidth: The number of characters in the resulting string
Returns
The input string padded
Examples
input Try out
hello{{ "world" | string.pad_left 10 }}
output
hello world
string.pad_right
string.pad_right <text> <width>
Description
Pads a string with trailing spaces to a specified total length.
Arguments
text: The input stringwidth: The number of characters in the resulting string
Returns
The input string padded
Examples
input Try out
{{ "hello" | string.pad_right 10 }}world
output
hello world
string.base64_encode
string.base64_encode <text>
Description
Encodes a string to its Base64 representation. Its character encoded will be UTF-8.
Arguments
text: The string to encode
Returns
The encoded string
Examples
input Try out
{{ "hello" | string.base64_encode }}
output
aGVsbG8=
string.base64_decode
string.base64_decode <text>
Description
Decodes a Base64-encoded string to a byte array. The encoding of the bytes is assumed to be UTF-8.
Arguments
text: The string to decode
Returns
The decoded string
Examples
input Try out
{{ "aGVsbG8=" | string.base64_decode }}
output
hello
string.index_of
string.index_of <text> <search> <startIndex>? <count>? <stringComparison>?
Description
Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions.
Arguments
text: The string to searchsearch: The string to find the index of.startIndex: If provided, the search starting position. If , search will start at the beginning of .count: If provided, the number of character positions to examine. If , all character positions will be considered.stringComparison: If provided, the comparison rules for the search. If , Allowed values are one of the following: 'CurrentCulture', 'CurrentCultureIgnoreCase', 'InvariantCulture', 'InvariantCultureIgnoreCase', 'Ordinal', 'OrdinalIgnoreCase'
Returns
The zero-based index position of the parameter from the start of if is found, or -1 if it is not. If value is , the return value is (if is not provided, the return value would be zero).