Oztaco
Oztaco

Reputation: 3459

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

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

Upvotes: 7

Views: 17815

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

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 ... "))
 {

 }

Upvotes: 1

Jared
Jared

Reputation: 6090

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

Upvotes: 14

Related Questions