PDA

View Full Version : concatenating three variables?



waxpants
July 14th, 2005, 01:23 PM
It's another simple problem that has me stumped.

x = 1;
y = 2;
z = 3;

trace(x+y+z);

how do I get this to display "123" instead of "6"?

I've tried both eval and concat() and have failed to get either to work, not that they don't work, I'm sure it's just me.

lunatic
July 14th, 2005, 01:27 PM
convert them to string first?

Lindquist
July 14th, 2005, 01:28 PM
Basically, you're going to have to convert the numbers to strings (a series of characters). You can either declare them as strings initially:

var x:String = "1";
var y:String = "2";
var z:String = "3";

trace(x+y+z);


Or convert them to strings when you need them:



var x:Number = 1;
var y:Number = 2;
var z:Number = 3;

trace(String(x)+String(y)+String(z));

ramie
July 14th, 2005, 01:30 PM
var output_string:String = String(x) + String(y) + String(z);
trace(output_string);

ramie
July 14th, 2005, 01:31 PM
lol, you beat me to it lindquist :)

waxpants
July 14th, 2005, 01:49 PM
trace(String(x)+String(y)+String(z));

beautiful!

Thankyou folks.

Lindquist
July 14th, 2005, 02:47 PM
lol, you beat me to it lindquist :)
in the game of kirupa, speed is the key! ;)

Smee
July 14th, 2005, 06:03 PM
You can also:


trace(""+x+y+z);