View Full Version : Length of String
Perlaki
03-23-2001, 09:51 AM
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
Jesse
03-23-2001, 11:20 AM
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
mad_A
03-23-2001, 11:40 AM
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
Jesse
03-23-2001, 12:11 PM
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
Perlaki
03-23-2001, 12:16 PM
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!
Perlaki
03-23-2001, 12:21 PM
I've just seen your method Mad_A, but I'd already applied Jesse's. Thanks anyway,
Simon
Jesse
03-23-2001, 12:22 PM
Lol. I'm afraid I've not had much experience with those sorts of Strings yet :)
Cheers
Jesse
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.