0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ What happens if you open the file in some text editor and let it show all non-printable characters? \$\endgroup\$ – wondra Oct 21 '19 at 16:40
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Oct 21 '19 at 18:07
  • \$\begingroup\$ @wondra I opened the file in notepad++ and it shows "CR" and "LF" after each line except for the last one \$\endgroup\$ – iQew Oct 21 '19 at 20:56
  • \$\begingroup\$ @DMGregory How do I print out the character code contents? \$\endgroup\$ – iQew Oct 21 '19 at 20:57
  • 1
    \$\begingroup\$ Debug.Log((int)myString[characterIndex]), no? My bet is that you're removing the LFs but not the CRs, so "--" doesn't match "--CR" \$\endgroup\$ – DMGregory Oct 21 '19 at 21:09
0
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Oct 22 '19 at 11:35
  • \$\begingroup\$ @DMGregory good idea, thank you! \$\endgroup\$ – iQew Oct 22 '19 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.