Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 756
  • Last Modified:

ActionScript 3.0 String Handling

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
0
Jason210
Asked:
Jason210
  • 9
  • 3
2 Solutions
 
CyanBlueCommented:
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
0
 
Jason210Author Commented:
Thanks. By way of an example, suppose I have a string "abcdefg" and I want to change it to "cde" ?
0
 
Jason210Author 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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jason210Author Commented:
sorry getting mixed up lease ignore last post
0
 
Jason210Author 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?
 
0
 
Jason210Author Commented:
What I mean is there a way to dynamically assign variable names based on for example, a loop counter?
0
 
CyanBlueCommented:
>> 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
0
 
Jason210Author 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...


 
 
 
 
0
 
Jason210Author 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.

 
0
 
Jason210Author Commented:
In AS2.0, you could do it with
this[someobject+i]:loader = new Loader();
0
 
CyanBlueCommented:
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

0
 
Jason210Author 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!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now