Format Code Now
/

Calculate String Length

TX-47

Paste a string and get its length three ways: characters as a reader sees them, UTF-16 code units, and UTF-8 bytes. A second view measures every line on its own.

Runs in your browser. Nothing is uploaded.

String or text

Length

How to use it

  1. Paste or type your string into the input box.
  2. Press Calculate length to see the character count, the UTF-16 length, the UTF-8 size and a short table of other counts.
  3. Press Length of each line to get one row per line, with characters, UTF-16 units and bytes.
  4. Use the share link if you want to send someone the same text.

Why one string has three lengths

A family emoji made of three people joined together is 1 character to a reader, 5 code points, 8 UTF-16 code units and 18 UTF-8 bytes. JavaScript, Java and C# report the UTF-16 count as the string length. Python counts code points, and Go and Rust report bytes. Databases and APIs may limit either characters or bytes, so the number you need depends on where the string is going. This page shows all of them side by side so you can pick the right one.

What is counted

Every character counts, including spaces and line breaks, and a line break at the end of the input counts too. Characters are grouped with the browser's Intl.Segmenter, so an accented letter or an emoji with a skin tone is one character. That grouping follows the Unicode version built into your browser, so a very new emoji can be counted slightly differently in an older browser. In the per-line view each line is measured without its own line break.

Checking limits for fields and files

Use the UTF-8 byte count for limits stated in bytes, such as many protocol fields and some database columns, and the character count for limits that talk about letters, such as a form field that allows 160 characters. For a JavaScript maxLength or a Java String length check, use the UTF-16 figure. Text with only ASCII characters has the same number for all of them.

Error messages and what to do

MessageWhat it meansFix
Paste the text to measure.You pressed Length of each line with nothing in the input box.Paste or type some text first. The first action accepts empty input and reports a length of 0.

Questions people ask

How do I calculate the length of a string?

Paste the string and press Calculate length. You get the number of characters as a reader sees them, the UTF-16 length that JavaScript, Java and C# return, and the UTF-8 size in bytes, plus word, line and sentence counts.

Why is my emoji longer than 1 in JavaScript?

JavaScript counts UTF-16 code units. Most emoji need two units, and an emoji made of several parts joined together needs many more. A family emoji is 1 character on screen but 8 in the JavaScript length.

What is the difference between string length and byte length?

Length counts characters or code units. Byte length counts the storage the text needs in an encoding. In UTF-8 an English letter takes 1 byte, an accented letter 2, most Chinese characters 3 and an emoji usually 4.

Does the string length include spaces and line breaks?

Yes. Spaces, tabs and line breaks all count as characters. The summary also shows characters without spaces, and the per-line view leaves out each line's own line break.

How do I get the length of every line in a text?

Press Length of each line. You get one row per line with its character count, UTF-16 length and UTF-8 bytes, then a total for the whole text with line breaks included.

About this tool

Length is given three ways because languages disagree. Characters are what a reader sees as one symbol, so a family emoji counts as 1. UTF-16 code units are what JavaScript, Java and C# report as the string length, so the same emoji is 8. UTF-8 bytes are what the text takes in a UTF-8 file or a network request. Line breaks count as characters in every figure, and a final line break at the end of the input is counted too. Each line is measured on its own in the per-line view, where the line break itself is not included. Characters are counted with the browser's Intl.Segmenter, so the grouping can differ slightly between browsers and versions. Nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.