How to read text file by particular line separator character?

String text = sr.ReadToEnd(); string lines = text. Split('\r'); foreach(string s in lines) { // Consume }.

1 for the simplicity and thinking outside the box – Adrian Jul 11 at 19:22 1 This is simple, but if the file contains 1 million lines this could end bad :) – pstrjds Jul 11 at 19:25 2 Yes, and if it hypothetically contained 10, 100, 1,000, or 10,000 it wouldn't be. Every answer has a hypothetical downside. ;) – George Jul 11 at 19:26 In my case, file can have tons of records... – User13839404 Jul 11 at 19:27 2 @pstrjds I understand that, but it really depends on his requirements.

If this solution doesn't work because of memory limitations, he can easily add a bit of code to stream in chunks of data, and split as needed, e.g. ReadBlock(). I'll leave it at that. He doesn't need to accept this answer, but it may be useful to others who may not face the same limitations.

:) – George Jul 11 at 19:34.

I would implement something like George's answer, but as an extension method that avoids loading the whole file at once (not tested, but something like this): static class ExtensionsForTextReader { public static IEnumerable ReadLines (this TextReader reader, char delimeter) { List chars = new List (); while (reader.Peek() >= 0) { char c = (char)reader. Read (); if (c == delimeter) { yield return new string(chars); chars. Clear (); continue; } chars.

Add(c); } } Which could then be used like: using (StreamReader sr = new StreamReader(FileName, Encoding. Default)) { foreach (var line in sr. ReadLines ('\n')) Console.

WriteLine (line); }.

According to the documentation: msdn.microsoft.com/en-us/library/system.... A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). By default the StreamReader ReadLine method will recognise a line by both/either \n or \r.

You could just use ReadToEnd() on the reader then use String. Split to delimit it anyway you see fit.

There's already an answer prior to your, with code. Avoid downvotes, since this can be considered a copy. – Adrian Jul 11 at 19:23.

You can use split method , more information on this page msdn.microsoft.com/en-us/library/system.... Bye.

Even though you said "Using StreamReader", since you also said "I my case, file can have tons of records...", I would recommend trying SSIS. It's perfect for what you're trying to do. You can process very large file and specify the line/column delimiters easily.

– pstrjds Jul 11 at 19:38 @pstrjds : Yes, I did mean Sql Server Integration Services :-D Sure it might be overkill, but what triggered my suggestion is really the "tons of records" part. Sometimes, I have to "parse" csv files that have around 18M lines and a ton of columns (about 450megs) and I like using SSIS for this. Of course, my usage is related to a SQL server too, but I like the tool (Even though I don't like some of it's interfaces/behaviors.

) – Tipx Jul 11 at 21:34.

This code snippet will read a line from a file until it encounters "\n". Using (StreamReader sr = new StreamReader(path)) { string line = string. Empty; while (sr.Peek() >= 0) { char c = (char)sr.Read(); if (c == '\n') { //end of line encountered Console.

WriteLine(line); //create new line line = string. Empty; } else { line += (char)sr.Read(); } } } Because this code reads character by character it will work with a file of any length without being constrained by available memory.

You either have to parse the stream byte-by-byte yourself and handle the split, or you need to use the default ReadLine behavior which splits on /r, /n, or /r/n. If you want to parse the stream byte-by-byte, I'd use something like the following extension method: public static string ReadToChar(this StreamReader sr, char splitCharacter) { char nextChar; StringBuilder line = new StringBuilder(); while (sr.Peek() > 0) { nextChar = (char)sr.Read(); if (nextChar == splitCharacter) return line.ToString(); line. Append(nextChar); } return line.

Length == 0? Null : line.ToString(); }.

Read text file by particular line separator character.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions