OK, after clarification of the question here's what might work for you
List<string> lines = new List<string>(); using (StreamReader reader = new StreamReader("Assets/test.txt")) { string lines = reader.ReadToEnd(); Debug.Log(lines); var splitText = lines.Split(new string[] { "|" }, // "\\n" in your case System.StringSplitOptions.RemoveEmptyEntries); string result = String.Empty; foreach (var dataEntry in splitText) { string dataToParse = dataEntry.Trim(); result += dataToParse + Environment.NewLine; } Debug.Log(result); }
Please note that if the file that you try to read is very big ReadToEnd might not work for you but for the sake of example it will do. So the logic here is to read the text file in a string, then use the Split method of string which splits the initial string by given array of delimiters and returns an array of strings, RemoveEmptyEntries means that there will be no empty strings in the resulting array. Now that you have your entries identified you can do with them what is required, in this case we Trim them (so there will be no leading and ending spaces) and we append the dataEntry to the result with a new line (please note that if you have a lot of data it will be better performance wise to use StringBuilder to append the results as it's faster than appending strings with + more info here: http://support.microsoft.com/kb/306822)
Before Edit
While reading from text file the stream reader recognizes the newlines and if u have text file with some lines of text your method will read them accurately, however if u have a single line text file with "\n" in it it will be no different than any two other characters, for example if you have text file containing a line like :
"text \n more text"
your method will get just one line with that string. So if you want you can manually parse the following string splitting it by "\n" and adding new lines manually to your display string (i can help you with that if that's what you want to achieve).
But if i were you i would just save the data in the text file with standard newlines which the reader will recognize, for example you could do something like:
List<string> dataToWrite = new List<string>(){ "firstLine", "secondLine", "thirdLine"}; using (StreamWriter writer = new StreamWriter("Assets/testWrite.txt")) { foreach (var line in dataToWrite) { writer.WriteLine(line); //writer.Write(line + Environment.NewLine); //writer.Write(line + "\r\n"); } }
Note that the two commented rows should work for you also, for more info regarding the new lines you could check the following link: http://social.msdn.microsoft.com/Forums/en-US/47af2197-26b4-4b9e-90e8-bfa9d5cd05b4/what-is-the-deference-between-r-n-and-rn-?forum=csharplanguage
I hope it helped, regards :)