# string-length

> Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes

Latest version **7.0.1** (published 2026-01-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install string-length
pnpm add string-length
yarn add string-length
bun add string-length
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 7.0.1 |
| Published | 2026-01-21 |
| First published | 2014-04-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 171 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | unicode, string, length, size, count, astral, symbol, surrogates, codepoints, ansi, escape, codes |

## Links

- npm: https://www.npmjs.com/package/string-length
- Repository: https://github.com/sindresorhus/string-length
- Homepage: https://github.com/sindresorhus/string-length#readme
- Issues: https://github.com/sindresorhus/string-length/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/string-length

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 7.0.1 (latest) — 2026-01-21
- 7.0.0 — 2026-01-21
- 6.0.0 — 2023-06-04
- 5.0.1 — 2021-09-13
- 5.0.0 — 2021-06-05
- 4.0.2 — 2021-03-17
- 4.0.1 — 2020-03-19
- 4.0.0 — 2020-02-21
- 3.1.0 — 2019-04-02
- 3.0.0 — 2019-03-18
- 2.0.0 — 2017-07-27
- 1.0.1 — 2015-07-16
- 1.0.0 — 2014-08-13
- 0.1.2 — 2014-05-01
- 0.1.1 — 2014-04-30
- … 1 more at https://npm.io/package/string-length/versions

## README

# string-length

> Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi)

`String#length` erroneously counts [astral symbols](https://web.archive.org/web/20150721114550/http://www.tlg.uci.edu/~opoudjis/unicode/unicode_astral.html) as two characters.

## Install

```sh
npm install string-length
```

## Usage

```js
import stringLength from 'string-length';

'🐴'.length;
//=> 2

stringLength('🐴');
//=> 1

stringLength('\u001B[1municorn\u001B[22m');
//=> 7
```

## API

### stringLength(string, options?)

#### options

Type: `object`

##### countAnsiEscapeCodes

Type: `boolean`\
Default: `false`

Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default.

## Related

- [string-length-cli](https://github.com/LitoMore/string-length-cli) - CLI for this module
- [string-width](https://github.com/sindresorhus/string-width) - Get visual width of a string

---
_Source: https://npm.io/package/string-length · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
