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 Jason B · Oct 07, 2011 at 09:58 PM · textloadingfileread

Confused about intermediate string reading.

I'm currently saving profile data to a text file. I'm not using PlayerPrefs because I prefer to have control over where data is saved, and the portability of that data.

However, I'm finding string functions to be confusing for what I'm trying to do.

For instance, if I want to save values A and B in a text file that's formatted like this:

 A=1000
 B=StringStuff

And then when I load the game back up, read all this and re-set my variables, how can I break all the read text into lines? They'll already be broken into lines when I save the file, but I'm not sure how to then read them as lines after the fact. Moreover, it seems most string functions require defining very specific character positions. For instance, if I want to load the value of B, I'd look for the characters after "B="... but I have no idea how I can possibly know the length beforehand, or figure it out between B and the line break so it doesn't also start reading stuff from C.

I'm probably thinking of this the wrong way, but any help would be appreciated. I've looked up C# tutorials for string reading but they aren't really helping out, as they aren't clear or concise about doing exactly what I want, but are instead very vague with no good examples.

If someone could show me what it might look like to read two separate lines from a text file and set variables using those values, it'd be appreciated greatly. I already understand saving it though.

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
1
Best Answer

Answer by DaveA · Oct 08, 2011 at 12:39 AM

String.Split http://msdn.microsoft.com/en-us/library/system.string.split.aspx

ex:

 var lines : string[] = myText.Split("\n"[0]); // split into lines
 for (var line in lines) // for each line
 {
   var parts = line.Split("="[0]); // split at the =
   var key = parts[0]; // eg "A"
   var value = parts[1]; // eg "1000"

and you have to parse numbers etc. using int.Parse, float.Parse etc. if you need that.

Comment
Add comment · Show 1 · 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 Jason B · Oct 08, 2011 at 02:27 AM 0
Share

Will I need to check for \r\n ins$$anonymous$$d of \n? Since \r\n is the only way plain text recognizes a line break.

Thanks for pointing me in the right direction, Dave.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I read read from and write to a text file? 2 Answers

Write and read text files with c# 1 Answer

Problem Reading A Text File 2 Answers

Best way to manage stats in a text file 2 Answers

Why is reading some text files with JS so darn slow?! (title changed to reflect answers) 1 Answer


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