Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by geroppo · Dec 29, 2012 at 10:43 PM · textfilesettingspolicy

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image geroppo · Dec 29, 2012 at 11:16 PM 0
Share

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!

avatar image Lovrenc · Dec 29, 2012 at 11:36 PM 0
Share

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.

avatar image geroppo · Dec 30, 2012 at 01:29 AM 0
Share

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 ?

avatar image Lovrenc · Dec 30, 2012 at 01:34 AM 0
Share

? 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")

avatar image geroppo · Dec 30, 2012 at 01:40 AM 0
Share

thanks for the explanation!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges