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 Cap · Jun 08, 2010 at 05:51 PM · stringintsplit

Extract number from string?

Say I have a string such as "20 Boxes" and I want to extract the number from that string as an integer to carry out some calculations on it. How should I go about this?

I'm used to using parseInt, but in Unity (unlike in JS) that won't accept any string that contains letters, so that's out unless I split the string up first. If I do have to split the string up first, what's the most efficient way to do so?

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

3 Replies

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

Answer by Tetrad · Jun 08, 2010 at 06:57 PM

You can use the String.Split method. Combine that with int.TryParse should get you what you want.

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 Eric5h5 · Jun 08, 2010 at 07:17 PM 0
Share

There's nothing specific to C# about that. Feel free to use String.Split and int.TryParse with Javascript and Boo.

avatar image Cap · Jun 08, 2010 at 07:53 PM 0
Share

Okay thanks, I'll just use Split(). Shame it can't take a string as a separator tho. Perhaps in version 3..

avatar image Eric5h5 · Jun 08, 2010 at 08:43 PM 0
Share

@Cap: it can use string[] as a separator, however. See the docs here: http://msdn.microsoft.com/en-us/library/1bwe3zdy.aspx

avatar image Cap · Jun 08, 2010 at 10:50 PM 0
Share

Hmm, okay. I find it odd that it would require an array containing a string for a single separator, rather than just a string. Something to file away in the brain for later anyway. Thanks.

avatar image
9

Answer by qJake · Jun 08, 2010 at 07:31 PM

You can also use Regex.Replace. This Regex find/replace pattern will remove all non-numbers from a given string.

If you're already familiar with Regex, just use this pattern:

[^0-9]

Which selects all non-numbered characters. Then, just replace with an empty string ("").

If you aren't familiar with Regex, this code should do it for you:

string numbersOnly = Regex.Replace("20 Boxes", "[^0-9]", "");

Remember to add the Using directive for the .NET Regex class:

using System.Text.RegularExpressions;

I would also like to point out, before all the Webplayer-lovers hound me for it, that the RegularExpressions library adds about 900k to the web player download, so if this is unacceptable for you, don't use Regex. ;)

Comment
Add comment · Show 11 · 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 Cap · Jun 08, 2010 at 07:37 PM 0
Share

I looked into RegEx, but I thought it would contain some overhead since it has to load in that library, as opposed to using split and parse JS functions. Do you know if there's any downside apart from that extra 900k (game won't be made for the web)?

avatar image qJake · Jun 08, 2010 at 07:39 PM 0
Share

No. The library is included and loaded during startup, and it loads so fast you probably wouldn't even notice it. The only downside to using Regex is that it adds the download overhead to the web player, but if you're publishing for the desktop (Windows/$$anonymous$$ac), it's a nonissue.

avatar image Cap · Jun 08, 2010 at 07:52 PM 0
Share

Okay, well I think I'll stick with Split() for this but RegEx might be necessary later on in the project. If I can avoid using it then I suppose it would be nice to have 900k less even for the desktop.

avatar image Eric5h5 · Jun 08, 2010 at 08:40 PM 0
Share

@Cap: the 900$$anonymous$$ extra is LZ$$anonymous$$A compressed for the webplayer...with standalones it's more like 3$$anonymous$$B. But who's going to notice a few $$anonymous$$B these days? Pretty much a non-issue like SpikeX said.

avatar image Cap · Jun 08, 2010 at 10:45 PM 0
Share

Yea, and it'll get compressed for distribution on the desktop anyway, but i'll shave stuff off anywhere I can. Bandwidth can get expensive.

Show more comments
avatar image
0

Answer by Edgars Adamovics · Dec 24, 2014 at 10:21 PM

 string sentence = "10 cats, 20 dogs, 40 fish and 1 programmer.";
 
 string[] digits= Regex.Split(sentence, @"\D+");
             foreach (string value in digits)
             {
                 int number;
                 if (int.TryParse(value, out number))
                 {
                     Debug.Log(value);
                 }
 
 
             }

Since I was in need of something similar adding this solution for future reference!

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

1 Person is following this question.

avatar image

Related Questions

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

parseInt givin' me issues! 2 Answers

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

How to split alphanumeric string into array of strings based from their type : alpha or numeric ? 1 Answer

if statement not returning true 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