# \[FMX\]Please some explanation on substr

**URL:** <https://forum.kirupa.com/t/fmx-please-some-explanation-on-substr/50557>\
**Category:** Uncategorized\
**Created:** [May 4, 2004, 12:04pm UTC](https://forum.kirupa.com/t/fmx-please-some-explanation-on-substr/50557 "2004-05-04T12:04:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dboers](https://avatars.discourse-cdn.com/v4/letter/d/bbe5ce/32.png) [@dboers](https://forum.kirupa.com/u/dboers)\
**Post date:** [May 4, 2004, 12:04pm UTC](https://forum.kirupa.com/t/fmx-please-some-explanation-on-substr/50557/1 "2004-05-04T12:04:11Z")

</div>

For quite some time I use the following code to control, behaviors of some buttons and other content:

```auto
for (var i in this) {
if (this*._name.substr(0, 4) == "btn_") {
setKnop(this*._name);
}
}
 
function setKnop(clip) {
var clip = eval(clip);
clip.onRollOver = function() {
new Color(clip).setRGB(0x000000);
}; 

```

Etc, etc.

I used this code because somebody adviced me to do so. So in this case btn\_ is always followed by some other name. Now I bought the book Foundation actionscript from FriendsOfEd and they advice to use btn as a suffix “\_btn” as well as Colin Moock did in his book Actionscript the definite guide.

Is there somebody who can explain me a little bit more about the use of “\_name, substr” etc

What i would like to find out is how the this code:

```auto
_name.substr (0, 4) 

```

would look like if I start working with btn as suffix, as the adviced in the mentioned books?

Thanks in advance 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 4, 2004, 12:25pm UTC](https://forum.kirupa.com/t/fmx-please-some-explanation-on-substr/50557/2 "2004-05-04T12:25:51Z")

</div>

when you trace mc.\_name, you’ll get the instance name back, in this case is “mc”…

then, it checks if the _name begins with "btn_" (substr(0,4)), if so, its a button (if you named al your buttons like “btn\_”+name)

then it will apply setKnop on every button that was returned…  
(do i make any sence here?)

from the reference:

> 

**MovieClip.\_name**

_Availability_

Flash Player 4.

_Usage_

myMovieClip\_name

_Description_

Property; returns the instance name of the movie clip specified by MovieClip.

> 

**String.substr**

_Availability_

Flash Player 5.

_Usage_

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.

_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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 4, 2004, 1:17pm UTC](https://forum.kirupa.com/t/fmx-please-some-explanation-on-substr/50557/3 "2004-05-04T13:17:48Z")

</div>

Yes that made very much sence 🙂 Thank you. 🙂 But now the question about when I use btn as suffix “\_btn” I tried it the way Colin Moock is describing it in the book (negative starting index) so I tried it like this:

```auto
for (var i in this){
if (_name.substr(-4) == "_btn"){
setButton(this*._name);
}
}

```

But that wasn’t working. What am I doing wrong here?

[edit] I found my mistake already. Thanks again for your explanation 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 4, 2004, 3:03pm UTC](https://forum.kirupa.com/t/fmx-please-some-explanation-on-substr/50557/4 "2004-05-04T15:03:15Z")

</div>

substr needs a start index and a length

here 2 examples on how to use it…

> myString = “Hello”;  
> myString.substr(0,2);  
> trace(myString);  
> // returns “He”;

when reversing, use the length of the string

> myString = “Hello”;  
> strLength = myString.length;  
> myString = myString.substr(strLength-2,2);  
> trace(myString);  
> // returns “lo”;
