0
\$\begingroup\$

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!

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

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – youGuestIt Mar 9 '18 at 1:52
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Mar 9 '18 at 1:54
  • \$\begingroup\$ 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 \$\endgroup\$ – youGuestIt Mar 9 '18 at 2:00
  • \$\begingroup\$ 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" \$\endgroup\$ – DMGregory Mar 9 '18 at 2:04
  • \$\begingroup\$ 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 \$\endgroup\$ – youGuestIt Mar 9 '18 at 2:09

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.