Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Zukas · Aug 03, 2015 at 12:03 AM · stringinputfieldinteger

How to split string into two integers?

I want to do UI InputField in which you'd enter minimum and maximum amounts, for example 3-7 and then trough script split them into two different integers.

It doesn't have to be fast by code, but I don't want it to be too messy either.

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 maccabbe · Aug 03, 2015 at 01:04 AM 0
Share

What have you tried?

avatar image Zukas · Aug 03, 2015 at 01:40 AM 0
Share

I've tried googling trough, but I've found totally irrelavant stuff such as string.split

Though, I've just got an idea to try split it to two strings without the "-" and then parse those two strings into integer. It's particularly late here so I'll go sleep and try this after I'm back to PC.

3 Replies

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

Answer by DonHaul · Aug 03, 2015 at 08:22 AM

Something like this should work

 string str = //contents of the inputfieldstring;
 string[] minmax= str.Split('-');
 int min= int.Parse (minmax[0]);
 int max= int.Parse (minmax[1]);
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 Zukas · Aug 04, 2015 at 11:07 AM 0
Share

Oh wow! So many great and somewhat similar responses. I'll accept yours because you were first, but I'll +1 others too, thanks!

avatar image
1

Answer by GiyomuGames · Aug 03, 2015 at 02:44 AM

You should split your string using regular expressions https://msdn.microsoft.com/en-us/library/8yttk7sy(v=vs.110).aspx

 string input = "3-7";
 string pattern = "-";            // Split on hyphens 
 string[] substrings = Regex.Split(input, pattern);

Then in your substrings object you should have "3" and "7" which you can easily put back into integers (Int32.Parse(substrings[0]) and Int32.Parse(substrings[1])). https://msdn.microsoft.com/en-us/library/bb397679.aspx

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

Answer by NeverHopeless · Aug 03, 2015 at 08:35 AM

Another simple possiblity is to use Split with convertAll. You don't have to extract and convert each element by yourself. Try like this:

 string sample = "3-7";
 int[] arr = System.Array.ConvertAll(sample.Split('-'), new System.Converter<string, int>(int.Parse));

Verify using:

 foreach (var element in arr) {
     Debug.Log(element);
 }

Now you can use it like:

 Debug.Log(arr[0]);   // Prints 3
 Debug.Log(arr[1]);   // Prints 7


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

25 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

Related Questions

Using Strings To Call Function 1 Answer

Why can't I save an array? 1 Answer

Convert string to text in InputField 1 Answer

Sequential GameObject Activation 2 Answers

Get string from InputField 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