View Full Version : extract some chars from a variable
zero_one
December 18th, 2004, 01:56 PM
I have a variable with the value "_level0.store.1" ... is there any function in flash with which i can just take the 15th character and remove everything that is before the "1" --> basically i'd like to use "1" to read that position from an array... therefore i want something that:
from:
_root.nodeFrom = this --> returns "_level0.store.1"
I would get:
positioninArray = 1
thanks
sticmann
December 18th, 2004, 09:18 PM
I think you'll like this...
myString.substr( start , [ length ])
Parameters
start An integer that indicates the position of the first character in myString to be used to create the substring. If start is a negative number, the starting position is determined from the end of the string, where the -1 is the last character.
length The number of characters in the substring being created. If length is not specified, the substring includes all of the characters from the start to the end of the string.
Returns
A substring of the specified string.
Description
Method; returns the characters in a string from the index specified in the start parameter through the number of characters specified in the length parameter. The substr method does not change the string specified by myString , it returns a new string.
Great function
-Joshua
zero_one
December 19th, 2004, 11:03 AM
that's what i want but i'm having some trouble in making it work in my program (i made it work in another simple program just to test it):
_root.nodeFrom = this;
test3 = _root.nodeFrom;
test4 = test3.substr(0, 2)
trace(test4);
trace (this);
and i'm getting as a result:
undefined
_level0.store.1
could it be that "this" does not return a valid string to work this function on it? if it is so, what can i do??
thanks
zero_one
December 19th, 2004, 12:07 PM
i managed to make it work by changing a line to
test3 = new String(_root.nodeFrom);
Voetsjoeba
December 19th, 2004, 12:12 PM
Wait a minute, you need to get some things straight. First of all, the following code:
_root.nodeFrom = this
does not store the string "_level0.store.1" in the variable _root.nodeFrom. It makes _root.nodeFrom a reference to this. If you trace it, it will output the path to the movieclip it references which is _level0.store.1 to let you know what it is pointing to, but that does not mean that that is the value of the variable. I know this may be confusing because if you trace strings, it outputs their value. Then again, that's because are not references to a movieclip.
That also explains why you get undefined. You can't take a substring of a movieclip. You are making _root.nodeFrom a reference to this, and the path is _level0.store.1. So the this is the movieclip 1 in the store movieclip on the main timeline (level0). So to get that name, you just need to use the _name property:
_root.nodeFrom = this._name
But, there's a but - a number is an invalid character for an instance name to begin with, just like they are invalid characters for variable names to begin with. So give your movieclips other names starting with a letter, then try the above code.
The code that made it work for you is not a very good coding practice. You're performing a data type error there.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.