Hi Misael,

That would be useful as ActionScript string concatenation operations are fairly clumsy with more than a few pieces. Unfortunately the AS String class doesn't seem to have such functionality. It's not so hard to roll your own, because you don't need to worry about the various types, you just need a single token and call toString() on all the arguments. Here's a sketch of something, but it's not very robust. You would have to add checking for zero-length arguments and other unexpected conditions. It might get a bit trickier also if you want to do various number formats like hexadecimal.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute"
        creationComplete="testFormat()">
        <mx:Script>
                <![CDATA[
                        import mx.controls.Alert;
                        
                        public function stringFormat (format:String, ... 
args):String
                        {
                                var parts:Array = format.split('%@');
                                
                                // Parts should have one more element that args.
                                if (parts.length != args.length + 1)
                                        throw new Error("Problem with your 
logic!");
                                
                                var output:String = parts[0];
                                
                                for (var i:Number = 0; i < args.length; i++) {
                                        output += args[i].toString();
                                        output += parts[i + 1];
                                }
                                
                                return output;
                        }
                        
                        public function testFormat ():void
                        {
var str:String = stringFormat("Test, %@, insertion, %@, of, %@ vars",
                                        42, 'a string', {prop:'test'});
                                        
                                Alert.show(str);
                        }
                ]]>
        </mx:Script>
</mx:Application>

Cheers,
Lach


On 07/11/2006, at 11:13 PM, Misael wrote:

Hi,

is there an utility class, function or anything in AS3 with similar functionality of C printf function? I need to format a string just like I do it with this function, along with all the capabilities which comes along (%d, %s, etc).

Thanks!

Reply via email to