0

I have a text file with several lines of strings, for example "Commander" and "44105". I am reading those into a file where I will then display them on the screen. I need to create newlines at certain points, but when I am using \n in the text file the program doesn't seem to recognize as being new line but just displays \n in the string when it is shown on string.

Reader = new StreamReader("Assets/CorrectAnswers.txt", Encoding.Default); while (!Reader.EndOfStream && turnOff == false) { lineReader = Reader.ReadLine(); if (problemIndex == questionIndex) { answer = lineReader; turnOff = true; } questionIndex += 1; } 

Everything else there works just fine, it just doesn't recognize \n as being a special character. I would greatly appreciate some help in figuring out how to fix this problem.

1 Answer 1

2

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

I think you may have misinterpreted my question- I am not trying to write several strings to a text file with each string being on its own line. Rather, I am trying to read in one long string that contains a newline indicator, and have the program recognize that at this point in the string, I want to display a line break. Sorry if I'm misunderstanding what you're presenting.
Yep i misinterpreted the question, actually i was wondering which one of the these two things you try to achieve :) I've updated my answer, note that there the delimiter is set to "|" but there is no reason it won't work with "\n" , or any other string for that matter, just be sure that the string is not present somewhere in your data, and if you use backslash you have to escape it like "\\n" Regards ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.