Count Number Of Lines In String
Count lines in any text string instantly. Get total lines, non-empty lines, empty lines, and per-line statistics with this free online tool.
Per-Line Breakdown
About
Miscounting lines in configuration files, CSV datasets, or code patches leads to off-by-one errors that cascade into broken parsers and corrupted imports. The problem is subtle: different operating systems encode line endings differently - Unix uses LF (0x0A), Windows uses CRLF (0x0D 0x0A), and legacy Mac systems use bare CR (0x0D). A naive count of \n characters misses entire line classes. This tool normalizes all three conventions before splitting, then reports total lines, non-empty lines, blank lines, and per-line length statistics. It handles the edge case where a trailing newline does not constitute an additional empty line in most editor conventions. Note: the tool treats the input literally - a single character with no line breaks is one line, and an empty input is zero lines.
Formulas
The line count is derived by splitting the input string S on the regular expression pattern that matches all standard line break sequences:
Total line count N equals the length of the resulting array:
Non-empty line count Nne filters lines where the trimmed length exceeds zero:
Average line length is computed as:
Where S = input string, N = total number of lines, linei = the i-th line after splitting, len() = character count of a line, trim() = removal of leading and trailing whitespace.
Reference Data
| Line Ending | Escape Sequence | Hex Code | Used By | Unicode Name |
|---|---|---|---|---|
| Line Feed (LF) | \n | 0x0A | Unix, Linux, macOS (10+) | U+000A |
| Carriage Return (CR) | \r | 0x0D | Classic Mac OS (pre-X) | U+000D |
| CR + LF | \r\n | 0x0D 0x0A | Windows, DOS, HTTP headers | U+000D U+000A |
| Next Line (NEL) | - | 0x85 | IBM mainframes (EBCDIC) | U+0085 |
| Line Separator (LS) | - | 0x2028 | Unicode standard | U+2028 |
| Paragraph Separator (PS) | - | 0x2029 | Unicode standard | U+2029 |
| Vertical Tab (VT) | \v | 0x0B | Some legacy terminals | U+000B |
| Form Feed (FF) | \f | 0x0C | Printers, page breaks | U+000C |
| Record Separator (RS) | - | 0x1E | Data interchange (ASCII) | U+001E |
| Null (NUL) | \0 | 0x00 | C string terminators | U+0000 |
| End of Text (ETX) | - | 0x03 | Legacy serial protocols | U+0003 |
| End of Transmission (EOT) | - | 0x04 | Terminal Ctrl+D | U+0004 |