Skip to content

Function.truncateMiddle

github-actions[bot] edited this page Nov 18, 2026 · 13 revisions

umt / truncateMiddle

Function: truncateMiddle()

truncateMiddle(string_, length, separator?): string

Defined in: String/truncateMiddle.ts:15

Truncate a string in the middle, keeping its head and tail. The result length (in UTF-16 code units, like truncate) is at most length. When length is not larger than the separator, the separator is cut to length.

Parameters

string_

string

The string to truncate

length

number

The maximum length of the result

separator?

string = "..."

The text placed between head and tail (default: "...")

Returns

string

The truncated string

Example

truncateMiddle("Hello World", 8); // "Hel...ld"
truncateMiddle("Hello World", 7, "~"); // "Hel~rld"
truncateMiddle("Hello", 10); // "Hello"
truncateMiddle("Hello World", 2); // ".."

API

Classes

Interfaces

Type Aliases

Variables

Functions

Clone this wiki locally