Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting strings using Environment.Newline leaves \n in most array items?

Tags:

c#

split

newline

I used MyString.Split(Environment.Newline.ToCharArray()[0]) to split my string from a file into different pieces. But, every item in the array, except the first one starts with \n after I did that? I know the way that I'm splitting by newlines is kind of "cheaty" for lack of a better word, so if there is a better way of doing this, please tell me...

Here is the file... enter image description here

like image 633
Oztaco Avatar asked Nov 18 '12 04:11

Oztaco


People also ask

How do I split a string into a newline?

Split String at Newline 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 .

What is environment newline?

The Enviornment. NewLine in C# is used to add newline. To set a new line in between words − str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!"; The following is the code −

How do you split a string into a new line in Python?

To split a string by newline character in Python, pass the newline character "\n" as a delimiter to the split() function. It returns a list of strings resulting from splitting the original string on the occurrences of a newline, "\n" .

How split a string by line in C#?

Using String. NewLine , which gets the newline string defined for the current environment. Another way to split the string is using a Unicode character array. To split the string by line break, the character array should contain the CR and LF characters, i.e., carriage return \r and line feed \n .


2 Answers

If you are wanting to maintain using the .Split() instead of reading a file in a line at a time you can do...

var splitResult = MyString.Split( new string[]{ System.Environment.NewLine },
    System.StringSplitOptions.RemoveEmptyEntries  );
    /* or System.StringSplitOptions.None if you want empty results as well */

EDIT:

The problem you were having is that in a non-unix environment the new-line "character" is actually two characters. So when you grabbed the zero index you were actually splitting on a carriage return...not the new-line character (\n).

Windows = "\r\n"

Unix = "\n"

Per http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx

like image 198
Jared Avatar answered Oct 05 '22 08:10

Jared


A newline in Windows is two characters (\r and \n). The Environment.Newline.ToCharArray()[0] expression specifies only one of those characters: \r. Therefore, the other character (\n) remains as a portion of the split string.

My I suggest you read your file using something like this:

public IEnumerable<string> ReadFile(string filePath)
{
   using (StreamReader rdr = new StreamReader(filePath))
   {
       string line;
       while ( (line = reader.ReadLine()) != null)
       {
          yield return line;
       }
   }
}

You might need more error handling, or to specify different file open option, or to pass a stream to method rather than the path, but the idea of using an iterator over the ReadLine() method is sound. The result is you can just use code like this:

 foreach (string line in ReadLine(" ... my file path ... "))
 {

 }
like image 31
Joel Coehoorn Avatar answered Oct 05 '22 08:10

Joel Coehoorn