Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split Environment.NewLine?

Tags:

c#

I want to count the number of lines in a text.

Below works fine:

int numLines = copyText.Split('\n').Length - 1;

However, I've been using System.Environment.NewLine in the whole of my code and when I try:

 int numLines = copyText.Split(System.Environment.NewLine).Length - 1;

It keeps bringing up a red wriggly line underneath stating cannot convert string to char. Been trying to rectify this but no luck. Does anyone have any ideas?

like image 914
BruceyBandit Avatar asked Jun 13 '15 23:06

BruceyBandit


People also ask

How do you split in newline?

To split a string by newline, call the split() method passing it the following regular expression as parameter - /\r?\ n/ . The split method will split the string on each occurrence of a newline character and return an array containing the substrings.

Can you split () by a newline Python?

split() method splits the string by new line character and returns a list of strings. The string can also contain \n characters in the string as shown below, instead of a multi-line string with triple quotes.

How do I split a string after a new line?

Split a string at a newline character. When the literal \n represents a newline character, convert it to an actual newline using the compose function. Then use splitlines to split the string at the newline character. Create a string in which two lines of text are separated by \n .

How do you split a space and a new line in Python?

Use split() method to split by delimiter. If the argument is omitted, it will be split by whitespace, such as spaces, newlines \n , and tabs \t . Consecutive whitespace is processed together. A list of the words is returned.


2 Answers

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.

like image 184
Jesse Good Avatar answered Oct 27 '22 00:10

Jesse Good


As @Jesse Good noted, there are several kinds of newlines that might appear in a string. A regular expression can be used to match the various kinds of newlines that might appear in a string:

var text = "line 1\rline 2\nline 3\r\nline 4";

/* A regular expression that matches Windows newlines (\r\n),
    Unix/Linux/OS X newlines (\n), and old-style MacOS newlines (\r).
    The regex is processed left-to-right, so the Windows newlines
    are matched first, then the Unix newlines and finally the
    MacOS newlines. */
var newLinesRegex = new Regex(@"\r\n|\n|\r", RegexOptions.Singleline);
var lines = newLinesRegex.Split(text);

Console.WriteLine("Found {0} lines.", lines.Length);

foreach (var line in lines)
  Console.WriteLine(line);

Output:

Found 4 lines.
line 1
line 2
line 3
line 4

like image 28
Chris R. Timmons Avatar answered Oct 27 '22 01:10

Chris R. Timmons