# unity & c# - File.ReadAllLines() vs TextAsset Split('\n')

I switched from one approach to the other in my code and I noticed that File.ReadAllLines() has a string length of equal to the alpha-numeric chars in the string, with exception of the last line of the file. I assume that is eol. String.Split('\n') however will result in that extra char for every string in the array, and so only matches ReadAllLines() on the final string. Am I write in assuming its an end of line character that's introduced on every string in the split version? How can I trim these? I don't know of any eol in the special chars. I tried to trim \n and \r but it didn't change the line lengths

--- further clarification as to why I put it here instead of stackoverflow

I am not familiar enough with the workings to be sure. I thought perhaps the results might be due to unity. The File.ReadAllLines() version works on the path to the txt file, the string.Split() version works on the TextAsset.text and the TextAsset is passed through the inspector as reference.

Sorry if I made the wrong decision in posting here!

• Voting to close because it is a general programming question and should probably be asked on StackOverflow instead. – Charanor Mar 9 '18 at 0:27
• Fair enough, I only added it here in case the fact its from a unity TextAsset had some impact on the results. – youGuestIt Mar 9 '18 at 0:38

You can test this for yourself by walking through the characters of each string and comparing their values, or printing them as integers to find out what the character is.

eg. Debug.Log((int)myString[characterIndex]);

Whipping up a quick sample, the String.Split() version includes an extra character 13 at the end - a Carriage Return character.

This is because I composed the text file on a Windows machine that uses CR LF at the end of each line, characters 13 and 10 respectively. Splitting on '\n' = LF strips out the latter character, but leaves the former, while ReadAllLines consumes both characters of the linebreak.

You can use "\r\n" as your delimiter to consume both characters together in the Windows order, or an array including multiple different newline possibilities if you're unsure whether the file might use a different/mixed convention.

• Split("\r\n") doesn't work, cant convert to char. I'll try the char array version :) I'm also getting an issue with running on android, I'm using a TextAsset reference passed through the inspector which works on the editor and pc build but not the android build. – youGuestIt Mar 9 '18 at 1:52
• You need to pass an array of delimiters if you want to use a string, even if you only want one delimiter entry. Your Android problem is a different question entirely. – DMGregory Mar 9 '18 at 1:54
• I tried it out and reran it, but now that I'm splitting by {'\r', '\n'} My same debug.log is printing out an extra blank line each time saying word.length of 0, which means I'm getting a blank string after each string entry in my string[], which is over a hundred thousand blank strings. But the length is correct this way – youGuestIt Mar 9 '18 at 2:00
• Well, of course you're getting blank strings. You told it you wanted to split on either '\r' or '\n' as individual characters, not the string "\r\n" — so it finds an \r, splits the string, then finds an \n and splits again, leaving an empty string in between. If you want to use a string delimiter combining both characters, pass a string[] containing "\r\n" – DMGregory Mar 9 '18 at 2:04
• That's what I thought you meant at first but when I tried that it said it cant convert to char, I'll try again - I didn't have a using System It's now got no errors – youGuestIt Mar 9 '18 at 2:09