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 Lemo76 · Oct 21, 2014 at 08:26 AM · arraystringint

Extracting Numbers from String Place JS

I'm trying to create a multidimensional array using only one string. Let's say the printed output of turn[45] is:

Turns45 Lives39 Posx76.899 Posy3.445

How would I go about extracting the string into four separate ints like this:

 Turns = 45
 Lives = 39
 Posx = 76.899
 Posy = 3.445

Thanks!

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

Answer by Epicwolf · Oct 21, 2014 at 05:52 PM

You could easily use string.Split for this, however, you would need to have some sort of separator between the variable names and values, like this:

 bigstring = "Turns=45 Lives=39 Posx=76.899 Posy=3.445";

Then you could split the string by spaces by doing this:

 // The [0] is needed in C#, but I dunno about js.
 stringarray = bigstring.Split(" "[0]);

Which would return a string array like this:

 [ "Turns=45" , "Lives=39" , "Posx=76.899" , "Posy=3.445" ]

Finally, you could split the strings in stringarray by "=", and store the values.

 // these lines of code take each element from the array, split the by the "=", and set the variable to
 // the second item in the array that returns.
 //
 // For example, for Turns, the first item ( "Turns=45" ) is split,
 // which returns this: [ "Turns" , "45" ]
 // Then the second item, the "45", is converted to an int: 45
 // and is finally stored in Turns.
 //
 // This proccess is repeated for each variable, except Posx and Posy are floats.
 //
 Turns = int.Parse(stringarray[0].Split("="[0])[1]);
 Lives = int.Parse(stringarray[1].Split("="[0])[1]);
 Posx = float.Parse(stringarray[2].Split("="[0])[1]);
 Posy = float.Parse(stringarray[3].Split("="[0])[1]);
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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert a string to int array in Unity C# 1 Answer

bool[string] = true; array possible? 2 Answers

Array - Convert Object into Int 5 Answers

Converting a string to an int 2 Answers

Convert Array to String 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