简体   繁体   English

从文本文件读取时,Unity C#识别\\ n

[英]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". 我有一个包含几行字符串的文本文件,例如“ Commander”和“ 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. 我需要在某些点创建换行符,但是当我在文本文件中使用\\ n时,程序似乎无法识别为换行符,而只是在字符串上显示\\ n时才在字符串中显示\\ n。

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. 那里的所有其他东西都可以正常工作,只是不能将\\ n识别为特殊字符。 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. 请注意,如果您尝试读取的文件很大,则ReadToEnd可能对您不起作用,但为示例起见,它将起作用。 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. 因此,这里的逻辑是读取字符串中的文本文件,然后使用字符串的Split方法,该方法将给定的定界符数组拆分初始字符串并返回字符串数组, RemoveEmptyEntries表示结果中将没有空字符串数组。 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 ) 既然您已经确定了条目,则可以使用它们进行所需的操作,在这种情况下,我们将它们Trim (这样就不会有开头和结尾的空格),并且将dataEntry追加到结果后,并换行(请注意,如果如果您有大量数据,则使用StringBuilder附加结果将是更好的性能选择,因为它比使用+附加字符串在此处添加更多信息更快: 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 : 从文本文件读取时,流阅读器会识别换行符,并且如果您的文本文件带有几行文本,则您的方法将准确地读取换行符,但是,如果您的单个行文本文件中带有“ \\ n”,则没有区别而不是其他两个字符,例如,如果您的文本文件包含以下行:

"text \\n more text" “文字\\ n更多文字”

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). 因此,如果您愿意,可以手动解析以下字符串,并用“ \\ n”将其分割,然后在显示字符串中手动添加新行(如果您要实现的话,我可以为您提供帮助)。

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 请注意,两行注释也应为您工作,有关新行的更多信息,您可以检查以下链接: http : //social.msdn.microsoft.com/Forums/en-US/47af2197-26b4-4b9e- 90e8-bfa9d5cd05b4 / rn和rn-forum = csharplanage之间的区别是什么

I hope it helped, regards :) 希望对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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