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 DavidDebnar · Feb 21, 2013 at 04:31 PM · stringint

Is it possible for parseInt("5+5") to return 10?

Hey guys, is it possible to parse a string into an int/float, while also applying all the basic math operations? (+,-,/,*)? Example

 var theString : String = "10*(9-6)+25/5";
 var theInt : int = parse(theString);
 
 print(theInt); //this should print 35.

Does a function that provides this functionality exist, or should I write my own?

  • David

Comment
Add comment · Show 1
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 Fattie · May 06, 2013 at 08:47 AM 0
Share

there has just got to be a library somewhere that does that!

2 Replies

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

Answer by numberkruncher · Feb 21, 2013 at 05:43 PM

You will usually need to implement an expression parser to evaluate such a string.

In UnityScript you might be able to use the eval function. But be very careful, many consider this function to be evil. You will likely need to use an exception trap. To improve matters you should pre-validate your input expression using a regex that only accepts the expected digit and symbol characters.

 function Awake() {
     try {
         var expression : String = '10 + 4 * (9-6)';
         var validatePattern : Regex = new Regex('^[0-9\\.+/*\\(\\)\\- ]+$');
         if (expression != '' && validatePattern.IsMatch(expression)) {
             var result : int = parseInt(eval(expression));
             Debug.Log(result);
         }
     }
     catch (e) {
         // handle the syntax/evaluation error
     }
 }
Comment
Add comment · Show 7 · 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 DavidDebnar · Feb 21, 2013 at 06:16 PM 0
Share

Your code doesn't work and isn't correct.

avatar image numberkruncher · Feb 21, 2013 at 06:36 PM 0
Share

@dzebna Sorry, it appears that UnityScript does not support regex literals. I will amend with the long-hand version...

avatar image DavidDebnar · Feb 21, 2013 at 07:22 PM 0
Share

This doesn't work either, it say 'Interface member implementation must be public or explicit' 3 times I think I will just drop this idea entirely then. I think it's an eval bug.

avatar image numberkruncher · Feb 21, 2013 at 07:29 PM 0
Share

I believe that 'Interface member implementation must be public or explicit' occurs when you have not implemented an interface member correctly.

 public class Something : ISomeInterface {
     // Option #1 : Public
     public void AnInterface$$anonymous$$ethod() {}
     // Option #2 : Explicit
     void ISomeInterface.AnInterface$$anonymous$$ethod() {}
 }

Note: The above is C# because I do not know the UnityScript syntax for option #2 :S

avatar image DavidDebnar · Feb 21, 2013 at 07:30 PM 0
Share

I tried setting the class and the Awake function to public, but that didn't work either :/

Show more comments
avatar image
1

Answer by Hannibalov · Feb 21, 2013 at 04:50 PM

I don't think that's included either in .net nor in unity. If you do the math with floats, however, you can always call .ToString() with the result. If for some reason you need to have strings, you should write your own parser

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

12 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

Related Questions

Extract number from string? 3 Answers

parseInt givin' me issues! 2 Answers

BCE0022: Cannot convert 'String' to 'int'. 0 Answers

Download a WWW int? Or convert? 1 Answer

GetHashCode() questions/Turning a text string into an int 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