Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
15
Question by ChimpKing · Dec 12, 2010 at 12:01 AM · stringtranslateint

How to turn a String to an Int?

I need to change a string into an integer. I know there is ToString() but is there something like ToInt()?

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

5 Replies

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

Answer by Eric5h5 · Dec 12, 2010 at 12:38 AM

parseInt (JS only), int.Parse (JS + C#), int.TryParse (JS + C#).

Comment
Add comment · Show 4 · 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 dissidently · Dec 21, 2010 at 07:03 PM 0
Share

this is not in the reference files provided by Unity. Is this normally common knowledge? It certainly wasn't to me, but I'm new to script.

avatar image Edy · Jan 29, 2013 at 11:11 AM 0
Share

(For the record) This is part of the $$anonymous$$ono API: http://docs.go-mono.com/?link=T%3aSystem.Int32%2f* int is equivalent to Int32.

avatar image Ghisallo · Sep 16, 2013 at 07:45 PM 1
Share

An example in JS:

 myInt = int.Parse(myString);
avatar image betobeast1246 · Apr 28, 2016 at 04:43 PM 1
Share

This is so confusing. Can anyone explain this to me?

avatar image
11

Answer by drudiverse · Aug 26, 2014 at 09:38 AM

if you want to go very fast and don't have hyphens and spaces and returns in string space:

 int.Parse("400")     123.07 ns
 IntParseFast("400")    2.87 ns

in .js it's simply this function to parse 50x faster: //js:

 function  IntParseFast( value : String) : int
 {
     var result : int = 0;
     for (var i = 0; i < value.Length; i++)
     {
         var letter : char = value[i];
         result = 10 * result + char.GetNumericValue(letter);
     }
     return result;
 }

this is the original CS code:

     public static int IntParseFast(string value)
     {
     int result = 0;
     for (int i = 0; i < value.Length; i++)
     {
         char letter = value[i];
         result = 10 * result + (letter - 48);
     }
     return result;
     }


  

probably have to use an extension:

 using UnityEngine;
 public static class Extns
     {
     public static int ParseFast( this string s )
         {
         int r = 0;
         for (var i = 0; i < s.Length; i++)
             {
             char letter = s[i];
             r = 10 * r;
             r = r + (int)char.GetNumericValue(letter);
             }
         return r;
         }
     }






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 berzerk · Jan 28, 2015 at 07:09 PM 0
Share

Very good, drudiverse! I added an overload to char as well based on your code:

     public static int IntParseFast(char value)
     {
         int result = 0;
         result = 10 * result + (value - 48);
         return result;
     }
avatar image
3

Answer by uhahaha · Dec 12, 2010 at 01:22 AM

Try:

var urInt: int = System.Int32.Parse(urText); print(urInt);

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 PointyPigheadGames · Oct 02, 2016 at 08:24 PM

@betobeast1246 So basicly what this code does is convert a string (alphanumeric) to an integer (numeric). parseInt only works with Javascript but int.Parse works with C# and Javascript. Here is some example code:

 function Submit () {
     answerInt = int.Parse(answer);
 }


So basically answerInt is your int variable and answer is your string. An example of this would be if you are making a calculator app and you wanted to subtract the contents of one input field from another. By default, the input field is stored as a string and using this code would convert it to an int, which can be added/subtracted. If you want decimals in your answer, use float instead. (float is like an int, but more precise).

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
0

Answer by thaiwas · May 07, 2018 at 04:22 PM

Remember to not try parsing string "0.1" with double quotes, or you get a silent crash

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

10 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

Related Questions

Error CS0029 fix 1 Answer

public string array controlled by an int in the inspector 1 Answer

PlayerPrefs Int to Float 1 Answer

Can I create a list with an int/float and a string? C# 2 Answers

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