Skip to main content

substring

$substring

Extracts a portion of a string by start index and optional length.

Parameters

  • start (number, required): Start index (0-based).
  • length (number, optional): Number of characters to extract. If omitted, extracts to end of string.

Example

// Extract the first 8 characters of a hash
new Schema('string').transformer({$substring: {start: 0, length: 8}})
// 'abcdef1234567890' → 'abcdef12'

// Strip a known prefix (e.g. 'Bearer ')
new Schema('string').transformer({$substring: {start: 7}})

// Extract a fixed-position field from a formatted string
new Schema('string').transformer({$substring: {start: 4, length: 2}})
// '2026-03-21' → '03' (month)