Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback


The Deno Standard Library
Go to Latest
function unicodeWidth

Use unicodeWidth from std/cli instead. This will be removed once the Standard Library migrates to JSR.

import { unicodeWidth } from "";

Calculate the physical width of a string in a TTY-like environment. This is useful for cases such as calculating where a line-wrap will occur and underlining strings.

The physical width is given by the number of columns required to display the string. The number of columns a given unicode character occupies can vary depending on the character itself.


Calculating the unicode width of a string

import { unicodeWidth } from "";

unicodeWidth("hello world"); // 11
unicodeWidth("天地玄黃宇宙洪荒"); // 16
unicodeWidth("fullwidth"); // 18

Calculating the unicode width of a color-encoded string

import { unicodeWidth } from "";
import { stripAnsiCode } from "";

unicodeWidth(stripAnsiCode("\x1b[36mголубой\x1b[39m")); // 7
unicodeWidth(stripAnsiCode("\x1b[31m紅色\x1b[39m")); // 4
unicodeWidth(stripAnsiCode("\x1B]8;;\x07🦕\x1B]8;;\x07")); // 2

Use stripAnsiCode to remove ANSI escape codes from a string before passing it to unicodeWidth.


str: string

The string to measure.



The unicode width of the string.