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 william9518 · Sep 03, 2013 at 08:10 PM · variablesstringsorting

Get multiple variables through a string.

So I have ONE returned string saying something along the lines of: 'Hunger:100 Thirst:100 Strength:19' etc, etc. I also have variables, integer variables, called Hunger, Thirst, and Strength, respectively to the string. How would I go to assigning the Hunger variable to the 'Hunger:100' where it says 100 and the same for the other variables? I know this may be a little complicated, and I can't think of a way to do it yet. Thanks in advance, william9518

Comment
Add comment · Show 2
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 ZenithCode · Sep 03, 2013 at 08:18 PM 0
Share

Do you have access to build the string in a different way? $$anonymous$$g put a pipe symbol between each variable type?

avatar image numberkruncher · Sep 03, 2013 at 08:39 PM 1
Share

Ideally I would probably go with a standard data format like JSON, X$$anonymous$$L, YA$$anonymous$$L; though the simplicity of a dual-delimited string is ideal for really basic data.

2 Replies

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

Answer by DannyB · Sep 03, 2013 at 08:24 PM

Before you try to solve this problem you have created, I would try to eliminate it. It is not a healthy habit to work like this.

If you can return a dictionary instead of a string, do it.

If for some reason, you must return a string, then, the next logical step would be to try to receive it as dictionary. Meaning, instead of having a variable hunger, have it stored in the dictionary, under the key "Hunger".

Now, if you can apply one of the above then it will be very easy with string.Split and a nice little loop:

For strings such as "Hunger:100:Health:200"

 string[] tokens = receivedString.Split(':');
 for( int i=0; i<tokens.Length-1; i+=2 ) {
     myDictionary.Add( tokens[i], tokens[i+1] );
 }


For strings such as "Hunger:100 Health:200"

 string[] tokens = receivedString.Split(' ');
 foreach( string token in tokens ) {
     string keyValuePair[] = token.Split(':');
     myDictionary.Add( keyValuePair[0], keyValuePair[1] );
 }


Finally, if you still wish to maintain your int hunger variable, then I would still use the above method, and after it is done, add lines such as:

 hunger = myDictionary["Hunger"]



Comment
Add comment · Show 2 · 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 ZenithCode · Sep 03, 2013 at 08:26 PM 0
Share

I totally agree. If you can fix the problem, go for it rather than patching up with strings.

avatar image william9518 · Sep 03, 2013 at 10:53 PM 0
Share

$$anonymous$$UST return string because I am getting data off a mySQL database through PHP and I'm just in PHP using die("Hunger:100 Thirst:200"); and stuff like dat :( I will try the dictionary thx

avatar image
0

Answer by numberkruncher · Sep 03, 2013 at 08:35 PM

The answer by @DannyB looks like its on the right track, though if I am not mistaken it is implemented incorrectly:

Edit: Lol, looks like @DannyB fixed their answer whilst I wrote my answer nm :)

 public static Dictionary<string, string> ParseKeyValueString(string input) {
     var data = new Dictionary<string, string>();
     foreach (string arg in input.Split(' ')) {
         var pair = arg.Split(':');
         data[pair[0]] = pair[1];
     }
     return data;
 }

Usage Example:

 var data = ParseKeyValueString("Hunger:100 Thirst:100 Strength:19");
 foreach (string key in data.Keys)
     Debug.Log(string.Format("{0} = {1}", key, data[key]));
Comment
Add comment · 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

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

19 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can one use of a static variable, change it globaly? 2 Answers

Showing GUI by pressing diffrent buttons? 1 Answer

Why do I have to call ToString() when fetching a String from another script? 2 Answers

TextField Query 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