Skip to content
On this page

String

Module to generate string related entries.

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.bannedChars?readonly LiteralUnion<AlphaChar, string>[] | string[]

An array with characters to exclude.

options.casing?Casing'mixed'

The casing of the characters.

options.length?number1

The number of characters to generate.

Returns: string

ts
faker.string.alpha(options: number | {
  bannedChars: readonly LiteralUnion<AlphaChar, string>[] | string,
  casing: Casing,
  length: number
} = {}): string
faker.string.alpha() // => "C"
faker.string.alpha() // 'b'
faker.string.alpha(10) // 'fEcAaCVbaR'
faker.string.alpha({ casing: 'lower' }) // 'r'
faker.string.alpha({ bannedChars: ['W'] }) // 'Z'
faker.string.alpha({ length: 5, casing: 'upper', bannedChars: ['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.bannedChars?readonly LiteralUnion<AlphaNumericChar, string>[] | string[]

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

options.casing?Casing'mixed'

The casing of the characters.

options.length?number1

The number of characters and digits to generate.

Returns: string

ts
faker.string.alphanumeric(options: number | {
  bannedChars: readonly LiteralUnion<AlphaNumericChar, string>[] | string,
  casing: Casing,
  length: number
} = {}): string
faker.string.alphanumeric() // => "y"
faker.string.alphanumeric() // '2'
faker.string.alphanumeric(5) // '3e5V7'
faker.string.alphanumeric({ casing: 'upper' }) // 'A'
faker.string.alphanumeric({ bannedChars: ['W'] }) // 'r'
faker.string.alphanumeric({ length: 5, bannedChars: ["a"] }) // 'x1Z7f'

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?number1

Length of the generated number.

options.prefix?string'0x'

Prefix for the generated number.

Returns: string

ts
faker.string.hexadecimal(options: {
  casing: Casing,
  length: number,
  prefix: string
} = {}): string
faker.string.hexadecimal() // => "0xc"
faker.string.hexadecimal() // '0xB'
faker.string.hexadecimal({ length: 10 }) // '0xaE13d044cB'
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'

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?booleanfalse

If true, leading zeros will be allowed.

options.bannedDigits?readonly LiteralUnion<NumericChar, string>[] | string[]

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

options.length?number1

The number of digits to generate.

Returns: string

ts
faker.string.numeric(options: number | {
  allowLeadingZeros: boolean,
  bannedDigits: readonly LiteralUnion<NumericChar, string>[] | string,
  length: number
} = {}): string
faker.string.numeric() // => "5"
faker.string.numeric() // '2'
faker.string.numeric(5) // '31507'
faker.string.numeric(42) // '56434563150765416546479875435481513188548'
faker.string.numeric({ length: 42, allowLeadingZeros: true }) // '00564846278453876543517840713421451546115'
faker.string.numeric({ length: 6, bannedDigits: ['0'] }) // '943228'

sample

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

Available since v8.0.0

Parameters

NameTypeDefaultDescription
lengthnumber10

Length of the generated string. Max length is 2^20.

Returns: string

ts
faker.string.sample(length: number = 10): string
faker.string.sample() // => "TXcoYpSoHZ"
faker.string.sample() // 'Zo!.:*e>wR'
faker.string.sample(5) // '6Bye8'

uuid

Returns a UUID v4 (Universally Unique Identifier).

Available since v8.0.0

Returns: string

ts
faker.string.uuid(): string
faker.string.uuid() // => "89bd9d8d-69a6-474e-8f46-7cc8796ed151"
faker.string.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'

Released under the MIT License,本站仅做翻译,如有错误,请向我反馈 QQ:385858750