简体   繁体   中英

Unity C# Recognizing \n when reading in from text file

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.

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-rn-and-rn-?forum=csharplanguage

I hope it helped, regards :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM