PDA

View Full Version : getting the numbers out of a string containing both text and numbers


Schultz2001
10-28-2001, 07:00 PM
:confused: In a Flash file I have a dinamic text saying "250 MB" (value changes on specific events), now I need the number "250" from this string, so I can calculate something with it. I tried the Number function but it doesn't work becouse it tries to convert the letters "MB" to numbers too. So if anyone can help me out , i'll be very gratefull, thanks !

tg
10-29-2001, 04:22 AM
is "MB" a constant amoung all possibilties?
if so then you can just strip off the last three characters (" MB" - this would include the space) and then just read what ever number is left.

Schultz2001
10-29-2001, 05:27 PM
But thats exactly my problem, I don't know how to do that. I tried doing it this way : Size - " MB" (where Size is the variable assined to the value of the dinamic text field stating "200 MB") but of course it doesn't work, so if you know of a way for this to work please help. Thank you !

tg
10-29-2001, 06:40 PM
you could convert the string to an array and take off the last three elements.

or go here (http://www.layer51.com/proto/d.asp?p=2&f=16) and grab this prototype that is basically doing the same thing. this one removes a string from a string with split.

Jesse
10-30-2001, 02:28 AM
Hehe we've overlooked the obvious:
trace (parseInt("250 MB", 10));
Long live parseInt!

Schultz2001
10-30-2001, 07:51 PM
I wouldn't have imagined it would be this simple, How could I forget that "parseInt" Function. Allthough I did manage to get it work using tg's tipps (thank you a lot tg), it's a lot faster this way. Well it's no use banging my head against the wall now. Thank you all for your time, you helped me a lot !

tg
10-30-2001, 09:43 PM
:eek: