String
Module to generate string related entries.
Overview
For a string containing just A-Z characters, use alpha()
. To add digits too, use alphanumeric()
. If you only want punctuation marks/symbols, use symbol()
. For a full set of ASCII characters, use sample()
. For a custom set of characters, use fromCharacters()
.
For strings of base-ten digits, use numeric()
. For other bases, use binary()
, octal()
, or hexadecimal()
).
You can generate standard ID strings using uuid()
or nanoid()
.
Related modules
- Emoji can be found at
faker.internet.emoji()
. - The
faker.helpers
module includes a number of string related methods.
alpha
Generating a string consisting of letters in the English alphabet.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Either the number of characters or an options instance. |
options.casing? | Casing | 'mixed' | The casing of the characters. |
options.exclude? | string | Array<AlphaChar | string> | [] | An array with characters which should be excluded in the generated string. |
options.length? | number | { min: number; max: number; } | 1 | The number or range of characters to generate. |
Returns: string
function alpha(
options:
| number
| {
length?:
| number
| {
min: number;
max: number;
};
casing?: Casing;
exclude?: ReadonlyArray<LiteralUnion<AlphaChar>> | string;
} = {}
): string;
Examples
faker.string.alpha() // 'b'
faker.string.alpha(10) // 'fEcAaCVbaR'
faker.string.alpha({ length: { min: 5, max: 10 } }) // 'HcVrCf'
faker.string.alpha({ casing: 'lower' }) // 'r'
faker.string.alpha({ exclude: ['W'] }) // 'Z'
faker.string.alpha({ length: 5, casing: 'upper', exclude: ['A'] }) // 'DTCIC'
Source
alphanumeric
Generating a string consisting of alpha characters and digits.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Either the number of characters or an options instance. |
options.casing? | Casing | 'mixed' | The casing of the characters. |
options.exclude? | string | Array<AlphaNumericChar | string> | [] | An array of characters and digits which should be excluded in the generated string. |
options.length? | number | { min: number; max: number; } | 1 | The number or range of characters and digits to generate. |
Returns: string
function alphanumeric(
options:
| number
| {
length?:
| number
| {
min: number;
max: number;
};
casing?: Casing;
exclude?: ReadonlyArray<LiteralUnion<AlphaNumericChar>> | string;
} = {}
): string;
Examples
faker.string.alphanumeric() // '2'
faker.string.alphanumeric(5) // '3e5V7'
faker.string.alphanumeric({ length: { min: 5, max: 10 } }) // 'muaApG'
faker.string.alphanumeric({ casing: 'upper' }) // 'A'
faker.string.alphanumeric({ exclude: ['W'] }) // 'r'
faker.string.alphanumeric({ length: 5, exclude: ["a"] }) // 'x1Z7f'
Source
binary
Returns a binary string.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.length? | number | { min: number; max: number; } | 1 | The number or range of characters to generate after the prefix. |
options.prefix? | string | '0b' | Prefix for the generated number. |
Returns: string
function binary(
options: {
length?:
| number
| {
min: number;
max: number;
};
prefix?: string;
} = {}
): string;
Examples
faker.string.binary() // '0b1'
faker.string.binary({ length: 10 }) // '0b1101011011'
faker.string.binary({ length: { min: 5, max: 10 } }) // '0b11101011'
faker.string.binary({ prefix: '0b' }) // '0b1'
faker.string.binary({ length: 10, prefix: 'bin_' }) // 'bin_1101011011'
Source
fromCharacters
Generates a string from the given characters.
Available since v8.0.0
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. | |
length | number | { ... } | 1 | The length of the string to generate. |
length.max | number | The maximum length of the string to generate. | |
length.min | number | The minimum length of the string to generate. |
Returns: string
function fromCharacters(
characters: string | ReadonlyArray<string>,
length:
| number
| {
min: number;
max: number;
} = 1
): string;
Examples
faker.string.fromCharacters('abc') // 'c'
faker.string.fromCharacters(['a', 'b', 'c']) // 'a'
faker.string.fromCharacters('abc', 10) // 'cbbbacbacb'
faker.string.fromCharacters('abc', { min: 5, max: 10 }) // 'abcaaaba'
Source
hexadecimal
Returns a hexadecimal string.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.casing? | Casing | 'mixed' | Casing of the generated number. |
options.length? | number | { min: number; max: number; } | 1 | The number or range of characters to generate after the prefix. |
options.prefix? | string | '0x' | Prefix for the generated number. |
Returns: string
function hexadecimal(
options: {
length?:
| number
| {
min: number;
max: number;
};
casing?: Casing;
prefix?: string;
} = {}
): string;
Examples
faker.string.hexadecimal() // '0xB'
faker.string.hexadecimal({ length: 10 }) // '0xaE13d044cB'
faker.string.hexadecimal({ length: { min: 5, max: 10 } }) // '0x7dEf7FCD'
faker.string.hexadecimal({ prefix: '0x' }) // '0xE'
faker.string.hexadecimal({ casing: 'lower' }) // '0xf'
faker.string.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1'
faker.string.hexadecimal({ length: 10, casing: 'upper' }) // '0xE3F38014FB'
faker.string.hexadecimal({ casing: 'lower', prefix: '' }) // 'd'
faker.string.hexadecimal({ length: 10, casing: 'mixed', prefix: '0x' }) // '0xAdE330a4D1'
Source
nanoid
Generates a Nano ID.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
length | number | { ... } | 21 | Length of the generated string. |
length.max | number | The maximum length of the Nano ID to generate. | |
length.min | number | The minimum length of the Nano ID to generate. |
Returns: string
function nanoid(
length:
| number
| {
min: number;
max: number;
} = 21
): string;
Examples
faker.string.nanoid() // ptL0KpX_yRMI98JFr6B3n
faker.string.nanoid(10) // VsvwSdm_Am
faker.string.nanoid({ min: 13, max: 37 }) // KIRsdEL9jxVgqhBDlm
Source
numeric
Generates a given length string of digits.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | number | { ... } | {} | Either the number of characters or the options to use. |
options.allowLeadingZeros? | boolean | true | Whether leading zeros are allowed or not. |
options.exclude? | string | Array<NumericChar | string> | [] | An array of digits which should be excluded in the generated string. |
options.length? | number | { min: number; max: number; } | 1 | The number or range of digits to generate. |
Returns: string
function numeric(
options:
| number
| {
length?:
| number
| {
min: number;
max: number;
};
allowLeadingZeros?: boolean;
exclude?: ReadonlyArray<LiteralUnion<NumericChar>> | string;
} = {}
): string;
Examples
faker.string.numeric() // '2'
faker.string.numeric(5) // '31507'
faker.string.numeric(42) // '06434563150765416546479875435481513188548'
faker.string.numeric({ length: { min: 5, max: 10 } }) // '197089478'
faker.string.numeric({ length: 42, allowLeadingZeros: false }) // '72564846278453876543517840713421451546115'
faker.string.numeric({ length: 6, exclude: ['0'] }) // '943228'
Source
octal
Returns an octal string.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.length? | number | { min: number; max: number; } | 1 | The number or range of characters to generate after the prefix. |
options.prefix? | string | '0o' | Prefix for the generated number. |
Returns: string
function octal(
options: {
length?:
| number
| {
min: number;
max: number;
};
prefix?: string;
} = {}
): string;
Examples
faker.string.octal() // '0o3'
faker.string.octal({ length: 10 }) // '0o1526216210'
faker.string.octal({ length: { min: 5, max: 10 } }) // '0o15263214'
faker.string.octal({ prefix: '0o' }) // '0o7'
faker.string.octal({ length: 10, prefix: 'oct_' }) // 'oct_1542153414'
Source
sample
Returns a string containing UTF-16 chars between 33 and 125 (!
to }
).
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
length | number | { ... } | 10 | Length of the generated string. |
length.max | number | The maximum number of characters to generate. | |
length.min | number | The minimum number of characters to generate. |
Returns: string
function sample(
length:
| number
| {
min: number;
max: number;
} = 10
): string;
Examples
faker.string.sample() // 'Zo!.:*e>wR'
faker.string.sample(5) // '6Bye8'
faker.string.sample({ min: 5, max: 10 }) // 'FeKunG'
Source
symbol
Returns a string containing only special characters from the following list:
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
length | number | { ... } | 1 | Length of the generated string. |
length.max | number | The maximum number of special characters to generate. | |
length.min | number | The minimum number of special characters to generate. |
Returns: string
function symbol(
length:
| number
| {
min: number;
max: number;
} = 1
): string;
Examples
faker.string.symbol() // '$'
faker.string.symbol(5) // '#*!.~'
faker.string.symbol({ min: 5, max: 10 }) // ')|@*>^+'
Source
ulid
Returns a ULID (Universally Unique Lexicographically Sortable Identifier).
Available since v9.1.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.refDate? | string | number | Date | faker.defaultRefDate() | The date to use as reference point for the newly generated ULID encoded timestamp. The encoded timestamp is represented by the first 10 characters of the result. |
Returns: string
function ulid(
options: {
refDate?: string | Date | number;
} = {}
): string;
Examples
faker.string.ulid() // '01ARZ3NDEKTSV4RRFFQ69G5FAV'
faker.string.ulid({ refDate: '2020-01-01T00:00:00.000Z' }) // '01DXF6DT00CX9QNNW7PNXQ3YR8'
Source
uuid
Returns a UUID v4 (Universally Unique Identifier).
Available since v8.0.0
Returns: string
function uuid(): string;
Examples
faker.string.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'