| Home | Tutorials | Forums | Articles | Blogs | Movies | Library | Employment | Press | Buy templates |
|
|
#1 |
|
Registered User
Join Date: Dec 2000
Location: Liverpool, England
Posts: 33
|
How long is a piece of string? Sorry, couldn't resist it.
I need to control the length of a string. I have a dynamic text box, that displays the velocity of my MC. Sometimes it shows up as 2.5, and sometimes 3.3423452352 Can I control it using one of the Functions>String functions. I've tried a few but am at a loss. Please help? Cheers Simon |
|
|
|
|
|
#2 |
|
Administrator
Join Date: Nov 2000
Location: Australia
Posts: 8,612
|
A string is twice the length from the middle to either end
![]() Now as for your problem, take a look at this sample script: myString = "123.456789"; decimals = 2; dotIndex = myString.indexOf( "." ) + 1; shortened = myString.substr( 0, dotIndex+decimals ); decimals is a variable which you set to the number of decimal places you want to show. Note this will totally screw up if there is no decimal point.. you'll have to add an If clause if that is at all likely, which cheks if: myString.indexOf( "." ) == -1; and if so, aborts. This means there is no dot in the orriginal string (indexOf returns -1 if it doesn't find the search string, which is a dot in this case). Cheers Jesse
__________________
Cheers Jesse Stratford ActionScript.org Cofounder Email: presented in this way to stop spam-bots: My email is composed of my first name (jesse) followed by my last name (stratford) followed by @ followed by actionscript.org Please don't email or PM me Flash questions, that's what the Forums are for! ![]() Please don't rely on me reading my PMs either. Email me about important stuff. |
|
|
|
|
|
|
|
|
#3 |
|
The Zen of ActionScript
Join Date: Mar 2001
Location: Dublin, Ireland
Posts: 713
|
you could also use the Math.round( ); action. It will round your number down to the nearest integer. Or if you want a string of an exact length you can use
substring(string, index, count);..for example, if myString = "onetwothree"; then writing... newString =substring(myString, 1, 6); would leave newString = "onetwo"; ie. six chars from the string. I think that is for flash 4, but you can use the myString.substring(from, to); in flash 5. A |
|
|
|
|
|
#4 |
|
Administrator
Join Date: Nov 2000
Location: Australia
Posts: 8,612
|
Like Arrays, Strings in Flash 5 are zero based so the first letter in a string is element Zero.
So if you wanted to use Mad_A's exmaple you would need to use myString.substring(0,6) Cheers Jesse
__________________
Cheers Jesse Stratford ActionScript.org Cofounder Email: presented in this way to stop spam-bots: My email is composed of my first name (jesse) followed by my last name (stratford) followed by @ followed by actionscript.org Please don't email or PM me Flash questions, that's what the Forums are for! ![]() Please don't rely on me reading my PMs either. Email me about important stuff. |
|
|
|
|
|
#5 |
|
Registered User
Join Date: Dec 2000
Location: Liverpool, England
Posts: 33
|
Nice one man, now I know how long my string is, plus I can control the length of my string. That's one more problem, solved, thanks a lot Jesse.
Do you know a way of controlling G Strings? That would be handy! |
|
|
|
|
|
#6 |
|
Registered User
Join Date: Dec 2000
Location: Liverpool, England
Posts: 33
|
I've just seen your method Mad_A, but I'd already applied Jesse's. Thanks anyway,
Simon |
|
|
|
|
|
#7 |
|
Administrator
Join Date: Nov 2000
Location: Australia
Posts: 8,612
|
Lol. I'm afraid I've not had much experience with those sorts of Strings yet
![]() Cheers Jesse
__________________
Cheers Jesse Stratford ActionScript.org Cofounder Email: presented in this way to stop spam-bots: My email is composed of my first name (jesse) followed by my last name (stratford) followed by @ followed by actionscript.org Please don't email or PM me Flash questions, that's what the Forums are for! ![]() Please don't rely on me reading my PMs either. Email me about important stuff. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| new string functions! tell me what you need! | colfaxrev | ActionScript 2.0 | 11 | 09-23-2008 10:59 PM |
| Limiting variable text string length | RQFlash | ActionScript 2.0 | 1 | 07-06-2006 04:16 PM |
| Parsing | mtanti | ActionScript 2.0 | 3 | 02-24-2006 04:37 PM |
| 06++ = 7 != 07 | Flash Gordon | Server-Side Scripting | 5 | 01-17-2006 07:27 PM |
| Can't get the length of a string | j1mmy | Simple Stuff (Newbies) | 5 | 10-11-2002 07:50 PM |