- Home /
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.
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.
There's nothing specific to C# about that. Feel free to use String.Split and int.TryParse with Javascript and Boo.
Okay thanks, I'll just use Split(). Shame it can't take a string as a separator tho. Perhaps in version 3..
@Cap: it can use string[] as a separator, however. See the docs here: http://msdn.microsoft.com/en-us/library/1bwe3zdy.aspx
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.
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. ;)
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)?
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.
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.
@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.
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.
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!
Your answer
Follow this Question
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