# Reading text from file changes the text?

I'm currently trying to load a simple text file and filter it by a certain string. In the text file I'm using a new line for each value and sometimes use a "--" to make things easier to read for me when viewing the .txt-file.

Now, when I load the file and split it by new lines I end up with a list of substrings, each representing a value I need. Plus, all the "seperators" I put into the file.

My goal is to clean the list from all the seperators and basically just remove them from the list. However, RemoveAll does not work and I found out that the strings containing "--" are not really "--", but something else, because using string.Equals("--") fails on them.

Somewhere when reading the data or splitting it, something happened to those parts, but I can't figure out what's happening.

Here's the code:

void Awake() {

    _fileLines = new List<string>();

TextAsset txt = Resources.Load<TextAsset>("SubjectPages/test");
_fileLines = new List<string>();
_fileLines = txt.text.Split(new string[] { "\n" }, System.StringSplitOptions.None).ToList<string>();

_fileLines.RemoveAll(x => ((string)x) == "");
_fileLines.RemoveAll(x => ((string)x) == "--");

for (int i = 0; i < _fileLines.Count; i++) {
Debug.Log(_fileLines[i] + " | " + (_fileLines[i].Equals("--")) + " | " + (_fileLines[i] == "--"));
}
}


Removing empty lines with RemoveAll seems to work fine.

• What happens if you open the file in some text editor and let it show all non-printable characters? – wondra Oct 21 '19 at 16:40
• Have you tried printing out the character code contents of one of the array entries you want to remove, just to find out what's in it? – DMGregory Oct 21 '19 at 18:07
• @wondra I opened the file in notepad++ and it shows "CR" and "LF" after each line except for the last one – iQew Oct 21 '19 at 20:56
• @DMGregory How do I print out the character code contents? – iQew Oct 21 '19 at 20:57
• Debug.Log((int)myString[characterIndex]), no? My bet is that you're removing the LFs but not the CRs, so "--" doesn't match "--CR" – DMGregory Oct 21 '19 at 21:09

## 1 Answer

I checked each character value for the lines containing "--" and they did indeed have a third character with a char value of 13. Looking it up this translates to "\r". So, instead of removing "--" from the list I am now removing "--\r" from the list and this works now :)

• Instead, you may want to split your lines on the pair \r\n rather than just \n. That will remove unwanted \r characters from all of your lines. – DMGregory Oct 22 '19 at 11:35
• @DMGregory good idea, thank you! – iQew Oct 22 '19 at 11:36