How to use the line counter
- Paste or type your text into the Text area.
- Decide whether to check Ignore trailing newline (matches most code editors).
- Read the results instantly: total lines, non-empty lines, blank lines, words and characters appear below.
How lines are counted
A line counter splits text at every newline character (\n) and counts the resulting segments. The string a\nb\nc produces three lines. If the text ends with a newline, such as a\nb\n, a naive split yields three segments where the last is empty. Most text editors treat that final newline as a terminator rather than a separator, so they still report two lines. The Ignore trailing newline option replicates this behaviour by removing one trailing newline before counting.
The formula for total lines when ignoring a trailing newline is straightforward. First, strip a single trailing \r\n, \n or \r if the option is enabled. Then split the remaining string on any newline sequence. For example, given the input Hello\nWorld\n with the option on, the tool removes the final newline to get Hello\nWorld, splits it into two segments, and reports two total lines. Without the option, the same input splits into three segments: "Hello", "World" and an empty string, yielding three total lines with one blank. An empty string always returns zero lines regardless of settings.
Reference table of common cases
The table below shows how different inputs are counted when the trailing newline option is enabled.
| Input (escaped) | Total lines | Non-empty | Blank |
|---|---|---|---|
"" | 0 | 0 | 0 |
"hello" | 1 | 1 | 0 |
"a\nb" | 2 | 2 | 0 |
"a\n\nb" | 3 | 2 | 1 |
"a\n" | 1 | 1 | 0 |
"\n" | 1 | 0 | 1 |
"\n\n" | 2 | 0 | 2 |
Practical uses for a line counter
Developers use a line counter to measure source file size, enforce style guide limits or estimate effort in legacy codebases. Many linters flag files that exceed a threshold such as 300 or 500 lines. Knowing the exact line count helps teams decide when to refactor a module into smaller pieces.
Writers and translators track progress by counting lines in drafts or subtitle files. Subtitle formats like SRT rely on precise line breaks, and an unexpected blank line can break playback. CSV and log analysts need to know row counts before importing data into spreadsheets or databases. When combined with a word counter, you get a full picture of document length and density.
Students submitting essays sometimes face line limits instead of word limits, especially in plain text environments or coding assignments. Checking the text line count before submission avoids formatting penalties.
Line endings across operating systems
Different systems represent newlines differently. Unix and macOS use a single line feed (LF, \n). Windows traditionally uses a carriage return plus line feed (CRLF, \r\n). Classic Mac OS used only a carriage return (CR, \r), though this is rare today.
This line counter normalises all three styles by splitting on /\r\n|\n|\r/, so pasting text from any platform gives accurate results. Mixed line endings within a single file are handled correctly because each recognised sequence counts as exactly one break. The order matters: the regular expression checks for \r\n first to avoid counting a Windows newline as two separate breaks.
When copying text from web pages, browsers often normalise line endings to \n. However, reading directly from a Windows log file might preserve \r\n. A reliable newline counter must handle both without double counting.
Worked examples
Each result below was produced by the same code the tool runs, so you can check your own numbers against them.
| Example | Input | Result |
|---|---|---|
| Short paragraph | Text: Hello world. / This is a test. / Three lines above.; Ignore trailing newline: yes | Total lines: 4; Non-empty lines: 3; Blank lines: 1; Words: 9; Characters: 48; Characters (no spaces): 39 |
| CSV data | Text: Name,Age,City / Alice,30,NYC / Bob,25,LA / Charlie,35,Chicago; Ignore trailing newline: yes | Total lines: 4; Non-empty lines: 4; Blank lines: 0; Words: 4; Characters: 55; Characters (no spaces): 52 |
| Code snippet | Text: function greet() { / console.log('hi'); / } / ; Ignore trailing newline: yes | Total lines: 3; Non-empty lines: 3; Blank lines: 0; Words: 5; Characters: 42; Characters (no spaces): 35 |
FAQ
Does an empty text have zero or one line?
An empty string has zero lines. A string containing only a newline character has one blank line (or zero if the trailing newline option is on and there is no other content).
What counts as a blank line?
A blank line is a line that contains no visible characters. Lines with only spaces or tabs are treated as non-empty by default, since they contain whitespace characters.
Why does my editor show a different line count?
Most editors ignore a single trailing newline at the end of a file. Enable the Ignore trailing newline checkbox in this line counter to match that behaviour.
Are Windows CRLF line endings supported?
Yes. The tool splits on CR, LF and CRLF sequences, so text copied from Windows, Linux or macOS will be counted correctly.
Is my text sent to a server?
No. All counting happens locally in your browser using JavaScript. Nothing is uploaded anywhere.
Can I count lines in a large file?
You can paste large texts directly into the field. Performance depends on your browser and device, but millions of characters are typically handled without issues.