Question
I need to split a string into separate lines in .NET. The main string method I know for this is Split, but I am not sure how to use it cleanly with newline characters. What is the best way to split a string by newlines?
For example, I want to handle text like this:
string text = "first line\r\nsecond line\nthird line\rfourth line";
What is the best approach in .NET to turn that into individual lines?
Short Answer
By the end of this page, you will understand how newline characters work in .NET, how to split strings into lines using Split, and how to handle different line ending formats such as \r\n, \n, and \r. You will also learn which approach is safest when text may come from different operating systems.
Concept
In .NET, a newline is not always represented by the same character sequence.
Common line endings are:
\r\n— Windows\n— Unix/Linux/macOS (modern)\r— older Mac systems
This matters because text can come from many sources:
- files
- APIs
- copied user input
- logs
- CSV or text processing tools
If you split only on one newline format, your code may work for some inputs and fail for others.
In C#, the string.Split() method can split on strings or characters. Since line endings may be one or two characters long, the most reliable beginner-friendly approach is usually to split on all common newline sequences.
A common pattern is:
string[] lines = text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
This tells .NET:
- Look for any of these line endings.
- Split the text wherever one appears.
- Return all parts as an array of strings.
If you want to ignore empty lines, use StringSplitOptions.RemoveEmptyEntries instead.
This concept matters because line-based processing is everywhere in real programming: reading configuration files, parsing logs, importing text data, and handling user-generated content.
Mental Model
Think of a long string as a sheet of paper with multiple written lines.
Newline characters are the invisible line break marks between those lines. Splitting by newlines is like cutting the paper at every line break so that each line becomes its own piece.
The tricky part is that different systems draw the line break mark differently:
- Windows uses a two-part mark:
\r\n - Unix uses a one-part mark:
\n - Older systems may use
\r
So instead of looking for only one kind of cut mark, good .NET code usually checks for all common ones.
Syntax and Examples
The basic syntax is:
string[] lines = text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
Example: keep all lines
string text = "first line\r\nsecond line\nthird line\rfourth line";
string[] lines = text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
foreach (string line in lines)
{
Console.WriteLine(line);
}
Output:
first line
second line
third line
fourth line
Example: remove empty lines
string text = "first line\n\nsecond line\r\n\r\nthird line";
string[] lines = text.Split(
new[] { "\r\n", "\n", "\r" },
StringSplitOptions.RemoveEmptyEntries
);
foreach (string line in lines)
{
Console.WriteLine(line);
}
Output:
Step by Step Execution
Consider this code:
string text = "apple\r\nbanana\ncarrot";
string[] lines = text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
Step by step
textcontains three words separated by line endings.- The first separator is
\r\n, so.Split()finds that betweenappleandbanana. - The second separator is
\n, so.Split()finds that betweenbananaandcarrot. - The string is cut into pieces at those positions.
- The result is an array:
lines[0] = "apple"
lines[1] = "banana"
lines[2] = "carrot"
Trace example with empty lines
Real World Use Cases
Splitting text into lines is common in many practical situations.
Reading text files
You may load a text file into memory and process each line:
string content = File.ReadAllText("data.txt");
string[] lines = content.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
Parsing logs
Application logs are often line-based. Splitting lets you inspect entries one by one.
Importing pasted user input
A user might paste one email address or product code per line. Your code can split the input into separate values.
Processing API responses
Some APIs or command-line tools return plain text with newline-separated records.
Building tools for configuration data
Simple config formats often store one value per line, so splitting is the first step before validation or parsing.
Real Codebase Usage
In real projects, developers usually do more than just call Split().
1. Validation before splitting
Check for null or empty input first:
if (string.IsNullOrEmpty(text))
{
return Array.Empty<string>();
}
string[] lines = text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
This is a common guard clause.
2. Remove empty lines when blank lines do not matter
string[] lines = text.Split(
new[] { "\r\n", "\n", "\r" },
StringSplitOptions.RemoveEmptyEntries
);
Useful when parsing lists of IDs, tags, or commands.
3. Trim each line after splitting
Real input often contains extra spaces:
string[] lines = text
.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Trim())
.ToArray();
Common Mistakes
Mistake 1: Splitting only on Environment.NewLine
string[] lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
Why this can fail:
- On Windows,
Environment.NewLineis usually\r\n - But your input may contain only
\n - Then the string will not split correctly
Use this only when you fully control the input format.
Mistake 2: Splitting on \n only and leaving \r
string[] lines = text.Split('\n');
If your text contains Windows line endings (\r\n), each line may end with an extra \r character.
Example problem:
string text = "one\r\ntwo";
string[] lines = text.Split('\n');
Console.WriteLine(lines[0] == "one");
Console.WriteLine(lines[]);
Comparisons
| Approach | Example | Best for | Limitation |
|---|---|---|---|
| Split on all common newline types | text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None) | Cross-platform text input | Slightly more verbose |
Split on Environment.NewLine | text.Split(new[] { Environment.NewLine }, StringSplitOptions.None) | Input known to match current OS | Can fail for text from another platform |
Split on \n only | text.Split('\n') | Quick scripts when input format is known | Can leave \r at end of lines |
Read line by line with StringReader |
Cheat Sheet
// Best general-purpose newline split
string[] lines = text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
// Ignore blank lines
string[] linesNoEmpty = text.Split(
new[] { "\r\n", "\n", "\r" },
StringSplitOptions.RemoveEmptyEntries
);
// Only when input definitely matches current OS newline
string[] envLines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
Common newline sequences
\r\n= Windows\n= Unix/Linux/macOS\r= older Mac format
Rules of thumb
- Use all common newline separators for cross-platform safety.
- Use
RemoveEmptyEntriesif blank lines should be ignored. - Check for
nullbefore callingSplit(). - Be careful when splitting only on
\n, because\rmay remain.
Handy safe pattern
FAQ
How do I split a string by line breaks in C#?
Use Split with all common newline sequences:
text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None)
Should I use Environment.NewLine to split lines?
Only if you know the input uses the same newline style as the current system. For cross-platform input, splitting on all common newline formats is safer.
Why does splitting on \n sometimes leave strange characters?
Because Windows lines usually end with \r\n. If you split only on \n, the \r may remain at the end of each line.
How do I ignore blank lines when splitting text?
Use StringSplitOptions.RemoveEmptyEntries.
What is the difference between \r, \n, and \r\n?
They are different line ending formats used by different operating systems and tools.
Is StringReader better than ?
Mini Project
Description
Build a small C# utility that takes a block of text, splits it into lines safely, removes blank lines, trims extra spaces, and prints the cleaned result. This demonstrates practical newline handling similar to what you would do when processing pasted user input or imported text.
Goal
Create a method that converts multiline text into a clean array of non-empty lines.
Requirements
- Accept a multiline string as input.
- Split the text using common newline formats.
- Remove empty lines.
- Trim whitespace from each line.
- Print the cleaned lines with their index numbers.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Array Initialization Syntaxes Explained
Learn all common C# array initialization syntaxes with examples, rules, comparisons, and mistakes beginners often make.