- Home /
txt file line endings differ from policy settings
Hello there,the question/problem is quite simple, i have a txt file with 5 lines of text (random crap ) separated by one blank line. I made the txt file in the windows notepad and then imported it into unity. Now the thing is that when i run this simple code:
public TextAsset txt;
string[] text;
void Start ()
{
//here i just split the text file into a string
//array, separated by two "new lines" (\n\n)
text=Regex.Split(txt.text,"\n\n");
// and here i just try to print the length of the new array
// to check if it splitted correctly
Debug.Log (text.Length);
}
As the txt file has 5 lines, the code should print 5, but it doesn't. The problem is that when i open the txt file in monoDevelop to modify it, when saving it says "line endings differ from policy settings" , with a button that says "correct it" and "keep changes" (basically do nothing and save what i modified). It doesn't matter what i modified, but the thing is that if i press "correct it" then the code print 5 as it should be, if not it keeps saying 1.
Now sorry for the long explanation but the question is why is that happening ? i know that i can modify the policy of monodevelop and change the lane endings from unix to microsoft windows, but that just stops the warning from pop up, it doesn't resolve the problem ( the code says 1 instead of 5 ).
Is there a way to solve it ? so i can just make a txt file, import it into unity and dont have to "correct it" every time i import a new txt file ? its not a big deal since its just pressing a button, but it annoys me the fact that i have to do it with every new txt file. Thanks and sorry for the long explanation.
Answer by Lovrenc · Dec 29, 2012 at 10:49 PM
Use:
text = Regex.Split(txt.text,"\\r?\\n");
or
>text = Regex.Split(txt.text,"(\\r?\\n){2}");
or whatever correct regex is to cover all possible line endings.
sorry but that doesn't throw a correct solution, that way it will split the entire string into 7 substrings ( tho its better than 1). this way i will have to search again for blank spaces to remove and doesn't seem to be very efficient. Thanks for your reply though!
7 is unusual number. I would expect 1,5 or 9, except if you have some blanks on some lines. Did you try the second one? I mean you can still do:
text = Regex.Split(txt.text,"\\r?\\n\\r?\\n");
or
> text = Regex.Split(txt.text,"\\r\\n\\r\\n|\\n\\n");
i guess.
yup the second one worked out ( "\\r\\n\\r\\n|\\n\\n" ), thanks! , may i ask you to explain the difference between the double and single bars ? ( either n or r). And do you know why that "correct thing" was appearing ?
? means matching of preceding character of expression is optional.
\\n
to escape backslash as it needs to be matched in regex. Could also be done with @"\n"
(as this is same as "\\n"
)
Your answer
Follow this Question
Related Questions
How does a TextAsset work? 1 Answer
Best way to manage stats in a text file 2 Answers
How to change the file name for the final build? 2 Answers
using json file for waypoints for vehicle in unity project. 0 Answers
How do you create a folder in c#? 3 Answers