String
Module to generate string related entries.
Overview
For a string containing just A-Z characters, use {{$string.alpha}}
. To add digits too, use {{$string.alphanumeric}}
. If you only want punctuation marks/symbols, use {{$string.symbol}}
. For a full set of ASCII characters, use {{$string.sample}}
. For a custom set of characters, use {{$string.fromCharacters}}
.
For strings of base-ten digits, use {{$string.numeric}}
. For other bases, use {{$string.binary}}
, {{$string.octal}}
, or {{$string.hexadecimal}}
).
You can generate standard ID strings using {{$string.uuid}}
or {{$string.nanoid}}
.
Related modules
Emoji can be found at {{$internet.emoji}}
.
The Helpers module includes a number of string related methods.
alpha
Generating a string consisting of letters in the English alphabet.
Parameters
Name | Type | Default | Description |
---|---|---|---|
casing | Casing | mixed | The casing of the characters. |
exclude | string | Array\<AlphaChar | string> | [] | An array with characters which should be excluded in the generated string. |
min | number | 1 | The minimum number of characters to generate. |
max | number | 1 | The maximum number of characters to generate. |
length | number | 1 | The range of characters to generate. |
Returns: string
Examples
{{$string.alpha}} // 'd'
{{$string.alpha(min=2,max=6)}} // 'DTK'
{{$string.alpha(casing='lower')}} // 'c'
{{$string.alpha(exclude="['W']")}} // 'U'
{{$string.alpha(min=3,max=6,casing='upper',exclude="['W']")}} // 'UQBASP'
alphanumeric
Generating a string consisting of alpha characters and digits.
Parameters
Name | Type | Default | Description |
---|---|---|---|
casing | Casing | mixed | The casing of the characters. |
exclude | string | Array\<AlphaNumericChar | string> | [] | An array with characters which should be excluded in the generated string. |
min | number | 1 | The minimum number of characters to generate. |
max | number | 1 | The maximum number of characters to generate. |
length | number | 1 | The range of characters to generate. |
Returns: string
Examples
{{$string.alphanumeric}} // 'L'
{{$string.alphanumeric(min=5,max=10)}} // 'AtULYXqbt'
{{$string.alphanumeric(casing='upper')}} // 'U'
{{$string.alphanumeric(exclude="['W']")}} // 'v'
{{$string.alphanumeric(min=3,max=8,exclude="['W']")}} // 'Qjb'
binary
Returns a binary string.
Parameters
Name | Type | Default | Description |
---|---|---|---|
prefix | string | 0b | Prefix for the generated number. |
min | number | 1 | The minimum number of characters to generate after the prefix. |
max | number | 1 | The maximum number of characters to generate after the prefix. |
length | number | 1 | The range of characters to generate. |
Returns: string
Examples
{{$string.binary}} // '0b0'
{{$string.binary(length=10)}} // '0b0010110101'
{{$string.binary(prefix='0b')}} // ‘0b0’
{{$string.binary(length=10,prefix='0b')}} // '0b1110010111'
fromCharacters
Generates a string from the given characters.
Parameters
Name | Type | Default | Description |
---|---|---|---|
characters | string | string[] | The characters to use for the string. Can be a string or an array of characters. If it is an array, then each element is treated as a single character even if it is a string with multiple characters. | |
min | number | The minimum length of the string to generate. | |
max | number | The maximum length of the string to generate. |
Returns: string
Examples
{{$string.fromCharacters}} // '7'
{{$string.fromCharacters(characters=['a','b','c'])}} // 'c'
{{$string.fromCharacters(characters=['a','b','c'],min=2,max=9)}} // ‘cabccb’
hexadecimal
Returns a hexadecimal string.
Parameters
Name | Type | Default | Description |
---|---|---|---|
casing | Casing | mixed | The casing of the characters. |
prefix | string | 0x | Prefix for the generated number. |
min | number | 1 | The minimum number of characters to generate after the prefix. |
max | number | 1 | The maximum number of characters to generate after the prefix. |
length | number | 1 | The range of characters to generate after the prefix. |
Returns: string
Examples
{{$string.hexadecimal}} // '0xe'
{{$string.hexadecimal(length=10)}} // '0x3b65dffeE2'
{{$string.hexadecimal(length=10,prefix='0x')}} // ‘0xFbbDc794d5’
{{$string.hexadecimal(length=10,casing='upper',prefix='0x')}} // '0xCD974CBDDD'
nanoid
Generates a Nano ID.
Parameters
Name | Type | Default | Description |
---|---|---|---|
min | number | 1 | The minimum length of the Nano ID to generate. |
max | number | 1 | The maximum length of the Nano ID to generate. |
length | number | 21 | Length of the generated string. |
Returns: string
Examples
{{$string.nanoid}} // '_q28ik2GPTEY1nZPxSv-T'
{{$string.nanoid(min=3,max=17)}} // 'fkzoc'
numeric
Generates a given length string of digits.
Parameters
Name | Type | Default | Description |
---|---|---|---|
exclude | string | Array\<NumericChar | string> | [] | An array of digits which should be excluded in the generated string. |
allowLeadingZeros | boolean | true | Whether leading zeros are allowed or not. |
min | number | 1 | The minimum of digits to generate. |
max | number | 1 | The maximum of digits to generate. |
length | number | 1 | The range of digits to generate. |
Returns: string
Examples
{{$string.numeric}} // '4-T'
{{$string.numeric(length=5)}} // '55812'
{{$string.numeric(min=2,max=7)}} // '495'
{{$string.numeric(exclude="['0']")}} // '7'
{{$string.numeric(allowLeadingZeros=false)}} // '6'
octal
Returns an octal string.
Parameters
Name | Type | Default | Description |
---|---|---|---|
min | number | 1 | The minimum of digits to generate after the prefix. |
max | number | 1 | The maximum of digits to generate after the prefix. |
length | number | 1 | The range of digits to generate after the prefix. |
prefix | string | 0o | Prefix for the generated number. |
Returns: string
Examples
{{$string.octal}} // '0o7'
{{$string.octal(length=10)}} // '0o5125125412'
{{$string.octal(min=5,max=10)}} // '0o7606725'
{{$string.octal(prefix='0o')}} // '0o5'
{{$string.octal(length=10,prefix='0o')}} // '0o5152675635'
sample
Returns a string containing UTF-16 chars between 33 and 125 (!
to }
).
Parameters
Name | Type | Default | Description |
---|---|---|---|
min | number | The minimum number of characters to generate. | |
max | number | The maximum number of characters to generate. |
Returns: string
Examples
{{$string.sample}} // 'D|50SZwZ80'
{{$string.sample(min=5,max=10)}} // 'SG={1:'
symbol
Returns a string containing only special characters from the following list:
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
Parameters
Name | Type | Default | Description |
---|---|---|---|
min | number | The minimum number of special characters to generate. | |
max | number | The maximum number of special characters to generate. |
Returns: string
Examples
{{$string.symbol}} // '_'
{{$string.symbol(min=3,max=8)}} // 'S}_"'
uuid
Returns a UUID v4 (Universally Unique Identifier).
Returns: string
Examples
{{$string.uuid}} // '06836237-fd6d-4fbc-bafa-4b9610139305'