views:

1102

answers:

3

How can i format a string with supplied variables in AS3?

//vars
var myNumber:Number = 12;
var myString:String = "Months";
var myObject:MovieClip = year;

//string
myString.txt = "One (?) consists of (?) consecutive (?)", string(myObject), string(myNumber), myString;

so in the string above, i would like myString to display "One year consists of 12 consecutive Months", but i'm new to AS3 and do not know how to properly format a string.

i'm sure that i'll have to cast the number variable into a string, string(myNumber), but i don't know if casting a movie clip variable to a string, string(myMovieClip), will return the name of the movie clip or produce an error. i'm willing to bet on the later.

+6  A: 

The answers to this similar question suggest using the Formatter class or StringUtil.substitute().

The latter looks the simplest; in your case you would use it like this:

var str:String = "One {0} consists of {1} consecutive {2}";
var newString:String = StringUtil.substitute(str, myObject, myNumber, myString);

(substitute() should automatically cast its arguments to String, but I'm not sure if, as in your code, you can cast a MovieClip--myObject--as a String.)

Another good option, especially if you've used printf in other programming languages, is this third-party printf-as3 function.

Jordan
+1  A: 

Hey,

Casting objects to strings

The method toString() is defined on the Object class. So all objects have this method defined for them. Calling myObject.toString() will therefore usually give you what you're looking for. Certain objects define additional methods, such as date.getHours(), which return string descriptions of the object in a different format from that supplied by getString().

For native types such as int, you can cast using String(myInt).

Concatenating strings together

You can then add together the different parts of a string as follows:

var myString:String = "There are " + String(24) + " hours in a day."

Hope that helps, Dave

Dave T
+1  A: 

The shorter way I'd do it is something like:

var finalString:String = "One " + myObject + " consists of " + myNumber + " " + myString;

A single or double quote initiates a string literal. If you use the + symbol to append something to a string literal it's going to automatically call toString() on that object.

myObject will return [Object MovieClip], though. What you want to do is create a custom class that extends MovieClip and then override the toString() protected method to return whatever string you want it to spit out.

Hope that helps!

Myk
myObject.name seems to work. var finalString:String = "One " + myObject.name + " consists of " + myNumber + " " + myString;
TheDarkInI1978
myObject.name will only work if A) myObject is on stage and has an instance name or if B) you manually set myObject.name somewhere previously. .name is a string property of any DisplayObject, I believe. It's there for better integration into the Flash IDE (ie so you can put stuff on stage) or for specific cases where you need to name stuff so you can use getChildByName(). Still, glad that helps! It's much easier to just use the + operator to assembly complex strings in Flash. Cheers!
Myk