Easiest Way to Split a String on Newlines in .Net

Easiest way to split a string on newlines in .NET?

To split on a string you need to use the overload that takes an array of strings:

string[] lines = theText.Split(
new string[] { Environment.NewLine },
StringSplitOptions.None
);

Edit:

If you want to handle different types of line breaks in a text, you can use the ability to match more than one string. This will correctly split on either type of line break, and preserve empty lines and spacing in the text:

string[] lines = theText.Split(
new string[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);

Split a string by a newline in C#

var result = mystring.Split(new string[] {"\\n"}, StringSplitOptions.None);

Since the new line is glued to the words in your case, you have to use an additional back-slash.

C# Unable to split string by new lines \n

Sometimes when you see a \n on screen it really is a backslash (ASCII 92 and an en(ASCII 110) not a placeholder/escape sequence for new line (ASCII 10) A big hint for that here is that text boxes will usually not display newlines with escape codes but will put in actual new lines.

To split on \n use the string "\\n" which represents a string of two characters: the two backslashes produce a single character ASCII 92 = '' in the string and then a lowercase n.

Alternately you could use @"\n". The @ sign tells C# not to use escape codes in the quoted string.

split strings into many strings by newline?

You could use the string.Split method. In particular I suggest to use the overload that use a string array of possible separators. This because splitting on the newline character poses an unique problem. In you example all the newline chars are simply a '\n', but for some OS the newline char is '\r\n' and if you can't rule out the possibility to have the twos in the same file then

string test = "2345\n564532\n345634\n234 234543\n1324 2435\n";
string[] result = test.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);

Instead if your are certain that the file contains only the newline separator allowed by your OS then you could use

string test = "2345\n564532\n345634\n234 234543\n1324 2435\n";
string[] result = test.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

The StringSplitOptions.RemoveEmptyEntries allows to capture a pair of consecutive newline or an ending newline as an empty string.

Now you can work on the array examining the last 3 digits of every string

foreach(string s in result)
{
// Check to have at least 3 chars, no less
// otherwise an exception will occur
int maxLen = Math.Min(s.Length, 3);
string lastThree = s.Substring(s.Length - maxLen, maxLen);

... work on last 3 digits
}

Instead, if you want to work only using the index of the newline character without splitting the original string, you could use string.IndexOf in this way

string test = "2345\n564532\n345634\n234 234543\n1324 2435\n";
int pos = -1;
while((pos = test.IndexOf('\n', pos + 1)) != -1)
{
if(pos < test.Length)
{
string last3part = test.Substring(pos - 3, 3);
Console.WriteLine(last3part);
}
}

How to split Environment.NewLine?

To split on newline, you can use the following:

copyText.Split(new string[] { System.Environment.NewLine },
StringSplitOptions.None).Length - 1;

Here is a reference to the overload which uses a string array.

Note that System.Environment.NewLine is of type System.String. On Windows it is a 2 character string: \r\n and on Unix systems it is a 1 character string: \n. This is why you cannot use it as a char.

Wikipedia has a good article on newlines:
https://en.wikipedia.org/wiki/Newline

I recommend reading it.

How do I seperate a string by new lines?

var secondLine = example.Split('\n')[1];

Best way to split string into lines

  • If it looks ugly, just remove the unnecessary ToCharArray call.

  • If you want to split by either \n or \r, you've got two options:

    • Use an array literal – but this will give you empty lines for Windows-style line endings \r\n:

      var result = text.Split(new [] { '\r', '\n' });
    • Use a regular expression, as indicated by Bart:

      var result = Regex.Split(text, "\r\n|\r|\n");
  • If you want to preserve empty lines, why do you explicitly tell C# to throw them away? (StringSplitOptions parameter) – use StringSplitOptions.None instead.

How to split a text file retaining newline character

You can use positive look behind:

string[] splittedFile = Regex.Split(fileString, "(?<=\r\n)");


Related Topics



Leave a reply



Submit