Strings
Function | Description | Meta |
---|---|---|
concat |
Joins a set or array of strings with a delimiter. Arguments: Returns:delimiter (string)string to use as a delimiter collection (any<array[string], set[string]>)strings to join output (string)the joined string | Wasm |
contains |
Returns Arguments: Returns:haystack (string)string to search in needle (string)substring to look for result (boolean)result of the containment check | Wasm |
endswith |
Returns true if the search string ends with the base string. Arguments: Returns:search (string)search string base (string)base string result (boolean)result of the suffix check | Wasm |
format_ |
Returns the string representation of the number in the given base after rounding it down to an integer value. Arguments: Returns:number (number)number to format base (number)base of number representation to use output (string)formatted number | Wasm |
indexof |
Returns the index of a substring contained inside a string. Arguments: Returns:haystack (string)string to search in needle (string)substring to look for output (number)index of first occurrence, | Wasm |
indexof_ |
Returns a list of all the indexes of a substring contained inside a string. Arguments: Returns:haystack (string)string to search in needle (string)substring to look for output (array[number])all indices at which | v0.37.0 SDK-dependent |
lower |
Returns the input string but with all characters in lower-case. Arguments: Returns:x (string)string that is converted to lower-case y (string)lower-case of x | Wasm |
replace |
Replace replaces all instances of a sub-string. Arguments: Returns:x (string)string being processed old (string)substring to replace new (string)string to replace y (string)string with replaced substrings | Wasm |
split |
Split returns an array containing elements of the input string split on a delimiter. Arguments: Returns:x (string)string that is split delimiter (string)delimiter used for splitting ys (array[string])split parts | Wasm |
sprintf |
Returns the given string, formatted. Arguments: Returns:format (string)string with formatting verbs values (array[any])arguments to format into formatting verbs output (string)
| SDK-dependent |
startswith |
Returns true if the search string begins with the base string. Arguments: Returns:search (string)search string base (string)base string result (boolean)result of the prefix check | Wasm |
strings. |
Returns true if any of the search strings begins with any of the base strings. Arguments: Returns:search (any<string, array[string], set[string]>)search string(s) base (any<string, array[string], set[string]>)base string(s) result (boolean)result of the prefix check | v0.44.0 SDK-dependent |
strings. |
Returns true if any of the search strings ends with any of the base strings. Arguments: Returns:search (any<string, array[string], set[string]>)search string(s) base (any<string, array[string], set[string]>)base string(s) result (boolean)result of the suffix check | v0.44.0 SDK-dependent |
strings. |
Returns the number of non-overlapping instances of a substring in a string. Arguments: Returns:search (string)string to search in substring (string)substring to look for output (number)count of occurrences, | v0.67.0 SDK-dependent |
strings. |
Renders a templated string with given template variables injected. For a given templated string and key/value mapping, values will be injected into the template where they are referenced by key. For examples of templating syntax, see https://pkg.go.dev/text/template Arguments: Returns:value (string)a templated string vars (object[string: any])a mapping of template variable keys to values result (string)rendered template with template variables injected | v0.59.0 SDK-dependent |
strings. |
Replaces a string from a list of old, new string pairs. Replacements are performed in the order they appear in the target string, without overlapping matches. The old string comparisons are done in argument order. Arguments: Returns:patterns (object[string: string])replacement pairs value (string)string to replace substring matches in output (string)string with replaced substrings | Wasm |
strings. |
Reverses a given string. Arguments: Returns:x (string)string to reverse y (string)reversed string | v0.36.0 Wasm |
substring |
Returns the portion of a string for a given Arguments: Returns:value (string)string to extract substring from offset (number)offset, must be positive length (number)length of the substring starting from output (string)substring of | Wasm |
trim |
Returns Arguments: Returns:value (string)string to trim cutset (string)string of characters that are cut off output (string)string trimmed of | Wasm |
trim_ |
Returns Arguments: Returns:value (string)string to trim cutset (string)string of characters that are cut off on the left output (string)string left-trimmed of | Wasm |
trim_ |
Returns Arguments: Returns:value (string)string to trim prefix (string)prefix to cut off output (string)string with | Wasm |
trim_ |
Returns Arguments: Returns:value (string)string to trim cutset (string)string of characters that are cut off on the right output (string)string right-trimmed of | Wasm |
trim_ |
Return the given string with all leading and trailing white space removed. Arguments: Returns:value (string)string to trim output (string)string leading and trailing white space cut off | Wasm |
trim_ |
Returns Arguments: Returns:value (string)string to trim suffix (string)suffix to cut off output (string)string with | Wasm |
upper |
Returns the input string but with all characters in upper-case. Arguments: Returns:x (string)string that is converted to upper-case y (string)upper-case of x | Wasm |
When using sprintf
, values are pre-processed and may have an unexpected type. For example,
%T
evaluates to string
for both string
and boolean
types. In such cases, use type_name
to
accurately evaluate the underlying type.
Examples
contains
contains
is a commonly used Rego built-in function that checks if a string contains a substring. The function returns true
if the string contains the substring and false
otherwise.
Some examples of policy use cases where contains
might be used include:
- Simple validation, such as checking if an email contains a
@
symbol. - Checking if user input contains restricted words or phrases for content moderation.
If you're looking to check a string that's expected to be at the start or end of a value, you might be better served by one of the following functions:
- Starts With: Checks if a string starts with a specified prefix.
- Any Prefix Match: Checks if a string starts with any of the specified prefixes.
- Ends With: Checks if a string ends with a specified suffix.
- Any Suffix Match: Checks if a string ends with any of the specified suffixes.
These are safer and potentially faster too.
contains
only operates on strings, if you're looking to check for the presence of a value in a list you cannot use this function.
If you're looking for the Rego keyword contains
for building multi-value rules, you can read
about it in the keywords section.
Simple email validation
In the example that follows, contains
is used to test if
the @
symbol is contained in the supplied email address. This can
be useful as a first check on raw user data.
package play
import rego.v1
example1 if contains("alice@example.com", "@")
example2 if contains("bob[at]example.com", "@")
example3 if contains(input.email, "@")
{
"email": "hello at example.com"
}
{}
Keyword checking for content moderation
The contains
function is also useful for checking user
text for keywords, certain keywords might not be allowed.
In this example, reasons
will be a list of the banned words
found in the input.message
.
package play
import rego.v1
banned_words := {"hate", "kill"}
reasons contains word if {
some word in banned_words
contains(input.message, word)
}
{
"message": "i hate bananas"
}
{}