Skip to content

substring

The substring() method returns a substring between two indexes without modifying the original string.

Interactive example

Syntax

Classic signature

typescript
function substring<
	GenericInput extends string
>(
	input: GenericInput,
	start: number,
	end?: number
): string

Curried signature

typescript
function substring<
	GenericInput extends string
>(
	start: number,
	end?: number
): (input: GenericInput) => string

Parameters

  • input: The string to extract from.
  • start: The index of the first character to include in the returned substring.
  • end (optional): The index of the first character to exclude from the returned substring. If omitted, extracts to the end of the string.

Return value

A new string containing the specified section of the original string.

See also

  • slice - Extracts a section of a string with negative index support
  • charAt - Returns the character at an index
  • indexOf - Finds the index of a substring

Sources

Released under the MIT license.