Skip to content

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().

alpha

Generating a string consisting of letters in the English alphabet.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Either the number of characters or an options instance.

options.casing?Casing'mixed'

The casing of the characters.

options.exclude?readonly Array<AlphaChar | string> | string[]

An array with characters which should be excluded in the generated string.

options.length?number | { max: number, min: number }1

The number or range of characters to generate.

Returns: string

ts
faker.string.alpha(options: number | {
  casing: Casing,
  exclude: readonly Array<AlphaChar | string> | string,
  length: number | {
  max: number,
  min: number
}
} = {}): string
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'

alphanumeric

Generating a string consisting of alpha characters and digits.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Either the number of characters or an options instance.

options.casing?Casing'mixed'

The casing of the characters.

options.exclude?readonly Array<AlphaNumericChar | string> | string[]

An array of characters and digits which should be excluded in the generated string.

options.length?number | { max: number, min: number }1

The number or range of characters and digits to generate.

Returns: string

ts
faker.string.alphanumeric(options: number | {
  casing: Casing,
  exclude: readonly Array<AlphaNumericChar | string> | string,
  length: number | {
  max: number,
  min: number
}
} = {}): string
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'

binary

Returns a binary string.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
options{ ... }{}

The optional options object.

options.length?number | { max: number, min: number }1

The number or range of characters to generate after the prefix.

options.prefix?string'0b'

Prefix for the generated number.

Returns: string

ts
faker.string.binary(options: {
  length: number | {
  max: number,
  min: number
},
  prefix: string
} = {}): string
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'

fromCharacters

Generates a string from the given characters.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
charactersstring | 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.

lengthnumber | { ... }1

The length of the string to generate.

length.maxnumber

The maximum length of the string to generate.

length.minnumber

The minimum length of the string to generate.

Returns: string

ts
faker.string.fromCharacters(characters: readonly string[] | string, length: number | {
  max: number,
  min: number
} = 1): string
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'

hexadecimal

Returns a hexadecimal string.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
options{ ... }{}

The optional options object.

options.casing?Casing'mixed'

Casing of the generated number.

options.length?number | { max: number, min: number }1

The number or range of characters to generate after the prefix.

options.prefix?string'0x'

Prefix for the generated number.

Returns: string

ts
faker.string.hexadecimal(options: {
  casing: Casing,
  length: number | {
  max: number,
  min: number
},
  prefix: string
} = {}): string
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'

nanoid

Generates a Nano ID.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
lengthnumber | { ... }21

Length of the generated string.

length.maxnumber

The maximum length of the Nano ID to generate.

length.minnumber

The minimum length of the Nano ID to generate.

Returns: string

ts
faker.string.nanoid(length: number | {
  max: number,
  min: number
} = 21): string
faker.string.nanoid() // ptL0KpX_yRMI98JFr6B3n
faker.string.nanoid(10) // VsvwSdm_Am
faker.string.nanoid({ min: 13, max: 37 }) // KIRsdEL9jxVgqhBDlm

numeric

Generates a given length string of digits.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Either the number of characters or the options to use.

options.allowLeadingZeros?booleantrue

Whether leading zeros are allowed or not.

options.exclude?readonly Array<NumericChar | string> | string[]

An array of digits which should be excluded in the generated string.

options.length?number | { max: number, min: number }1

The number or range of digits to generate.

Returns: string

ts
faker.string.numeric(options: number | {
  allowLeadingZeros: boolean,
  exclude: readonly Array<NumericChar | string> | string,
  length: number | {
  max: number,
  min: number
}
} = {}): string
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'

octal

Returns an octal string.

Available since v8.0.0

Parameters

NameTypeDefaultDescription
options{ ... }{}

The optional options object.

options.length?number | { max: number, min: number }1

The number or range of characters to generate after the prefix.

options.prefix?string'0o'

Prefix for the generated number.

Returns: string

ts
faker.string.octal(options: {
  length: number | {
  max: number,
  min: number
},
  prefix: string
} = {}): string
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'

sample

Returns a string containing UTF-16 chars between 33 and 125 (! to }).

Available since v8.0.0

Parameters

NameTypeDefaultDescription
lengthnumber | { ... }10

Length of the generated string.

length.maxnumber

The maximum number of characters to generate.

length.minnumber

The minimum number of characters to generate.

Returns: string

ts
faker.string.sample(length: number | {
  max: number,
  min: number
} = 10): string
faker.string.sample() // 'Zo!.:*e>wR'
faker.string.sample(5) // '6Bye8'
faker.string.sample({ min: 5, max: 10 }) // 'FeKunG'

symbol

Returns a string containing only special characters from the following list:

txt
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

Available since v8.0.0

Parameters

NameTypeDefaultDescription
lengthnumber | { ... }1

Length of the generated string.

length.maxnumber

The maximum number of special characters to generate.

length.minnumber

The minimum number of special characters to generate.

Returns: string

ts
faker.string.symbol(length: number | {
  max: number,
  min: number
} = 1): string
faker.string.symbol() // '$'
faker.string.symbol(5) // '#*!.~'
faker.string.symbol({ min: 5, max: 10 }) // ')|@*>^+'

uuid

Returns a UUID v4 (Universally Unique Identifier).

Available since v8.0.0

Returns: string

ts
faker.string.uuid(): string
faker.string.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'

Released under the MIT License.