We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

ActionScript 3.0 String Handling

Jason210
Jason210 asked
on
Medium Priority
766 Views
Last Modified: 2012-05-06
Hi

Does anyone know where I can find some good tutorials on string handling in ActionScript 3.0?

I need to know how to:

1) Convert a string varibale to a number variable (so the string becomes a variable name)

2) Convert a number variable to a string variable

3) Manipulate strings by selecting only certain characters and making them into a new varibale

4) adding strings to strings

5) taking strings off other strings

Thanks
Comment
Watch Question

Commented:
1) Use Number()/parseInt()/parseFloat() function...

var s:String = "123";
var n:Number = parseInt(s);
// or
var n:Number = Number(s);
trace(typeof(n) + " : " + n);

2) Use toString() function or simply add empty string...

var n:Number = 123;
var s:String = n.toString();
trace(typeof(s) + " : " + s);
// or
var s:String = n + "";

3) Use substr()/substring() to do so...

var s:String = "ActionScript";
var s1:String = s.substr(6);
trace(s1);

4) Use + operator...

var s1:String = "Action";
var s2:String = "Script";
var s3:String = s1 + s2;
trace(s3);

5) How is that different from 3)???  At any rate, use the same functions to do the job...

CyanBlue

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks. By way of an example, suppose I have a string "abcdefg" and I want to change it to "cde" ?

Author

Commented:
Hmmm...why won't this work?

 

var thumbName:String = "thumb"
var thumbNumber:Number
var thumbFull:Number
var thumbFileName:String 
for (var i:Number = 0; i < 10; i++)
  {
      thumbNumber = parseInt(i);
   trace(thumbNumber)
   //thumbFileName = 
   //var thumb2:Loader = new Loader();
   //thumb2.load(new URLRequest("images/thumb2.jpg"));
   //trace thumbNumber
  } 

Open in new window

Author

Commented:
sorry getting mixed up lease ignore last post

Author

Commented:
One more question here concerning strings.
Suppose I have a string variable called thumbFull, and suppose it's got a value of "thumb5"
What I want to do use the contents of the variable as a name for new, movie clip variable, so that I can create variable names at runtime. Is this possible?
 

Author

Commented:
What I mean is there a way to dynamically assign variable names based on for example, a loop counter?
Commented:
>> I have a string "abcdefg" and I want to change it to "cde" ?

var s:String = "abcdefg";
var s1:String = s.substr(2, 3);
trace(s1);

>> What I want to do use the contents of the variable as a name for new, movie clip variable, so that I can create variable names at runtime. Is this possible?
>> is there a way to dynamically assign variable names based on for example, a loop counter?

Sorry...  I don't really understand the question...  Got some example that can help me understand the problem better???  I don't mean the code example though...  I want some example that can help me understand the problem...

CyanBlue

Author

Commented:
Ok, thanks for the example above. That's really nice.
Now for a better explanation of the other problem.
Suppose I want to dyanamically load 10 jpeg thumbnails. I have given the files names "image0.jpg", "image1.jpg", "image2.jpg", image3.jpg" and so on.
Now, I might could write out the code to do it ten times, like this:
var image0:loader = new Loader ();
image0.load(new URLRequest ("images/image0.jpg);
But it would be much better if I could write out it out in a loop, and have the actionScript assign names to the variables. Then I'd have to use something like this :
var [name+i] :loader = new Loader();
As I understand square brackets, from page 178 of Moock's O'reilly guide, they can be used to access a variable so long as they return a string name. It doesn't seem to be able to create the variable though...
Hope this helps!
I might be going entirely the wrong way about it all...


 
 
 
 

Author

Commented:
I have arranged several empty movieclip symbols on the stage. I have files in a folder named "image0.jpg", "image1.jpg", "image2.jpg", image3.jpg" and so on.

Now, I could write out the code to do it ten times, like this:

var image0:loader = new Loader ();
image0.load(new URLRequest ("images/image0.jpg);

But it would be much better if I could write out it out in a loop, and have the actionScript assign names to the variables. I feel I'd have to use something like this :

var [name+i] :loader = new Loader();

This is where I'm really stuck. I really want to know how to create / refer to variable names dynamically at runtime.

 

Author

Commented:
In AS2.0, you could do it with
this[someobject+i]:loader = new Loader();

Commented:
Well...  It seems like you are turning one question to another...  But I guess it does not matter...  ;)

First of all, you cannot type a dynamic variable...  This line is not going to work...
var [name+i] :loader = new Loader();

I'd probably do something like this...

CyanBlue
for (var i:uint = 0 ; i < 5 ; i++)
{
     var loader:Loader = new Loader();
     loader.load(new URLRequest("images/image" + i + ".jpg");
}

Open in new window

Author

Commented:
I guess you're right. The reason is that many years ago I used to program in basic, and in basic you could make variable names by splicing and adding strings. Which was what I originally wanted to do.
I will award points to the answers given to my original question. Thanks for the last tip though!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.