08
Sep
07

Substituting values in strings using the Flex StringUtil class’s substitute() method

The following example shows how you can substitute values in a string with specified values using the static StringUtil.substitute() method in Flex. Similar to the printf() and sprintf() methods in other languages.

Full code after the jump.

Before getting right to the awkward example, lets look at the method signature from the documentation: “Flex 3 Language Reference : StringUtil.substitute()“.

public static function substitute(str:String, ... rest):String {...}

As we can see, this method takes a variable number of parameters (the “… rest” part), with the first parameter being a String object. This string can contain special placeholders such as {0} and {1} which will map to the additional parameters supplied to the method in the ... rest bit there.

Since a code snippet is worth a 1000 words, here’s a super simple example:

var result:String = StringUtil.substitute("The {0} brown {1}", "quick", "fox");
trace(result); // The quick brown fox

So, the {0} in the str parameter gets replaced by “quick” (the first item in the ... rest parameter) and likewise the {1} gets replaced by “fox”.

Hopefully that made a bit of sense, onward to my awkward example which lets you generate fun form letters!

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/08/substituting-values-in-strings-using-the-flex-stringutil-classs-substitute-method/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.utils.StringUtil;

            private function button_click(evt:MouseEvent):void {
                textArea.text = StringUtil.substitute(template,
                                    var0.text,
                                    var1.text,
                                    var2.text,
                                    var3.text,
                                    var4.text,
                                    var5.text);
            }
        ]]>
    </mx:Script>

    <mx:String id="template" source="template.txt" />

    <mx:HBox width="100%">
        <mx:Form>
            <mx:FormItem label="0:">
                <mx:TextInput id="var0" text="John Doe" />
            </mx:FormItem>
            <mx:FormItem label="1:">
                <mx:TextInput id="var1" text="death" />
            </mx:FormItem>
            <mx:FormItem label="2:">
                <mx:TextInput id="var2" text="Mega Insurance Inc." />
            </mx:FormItem>
            <mx:FormItem label="3:">
                <mx:TextInput id="var3" text="policy" />
            </mx:FormItem>
            <mx:FormItem label="4:">
                <mx:TextInput id="var4" text="terminated" />
            </mx:FormItem>
            <mx:FormItem label="5:">
                <mx:TextInput id="var5" text="serve you" />
            </mx:FormItem>
            <mx:FormItem direction="horizontal">
                <mx:Button id="button"
                        label="Substitute"
                        click="button_click(event)" />
                <mx:Button label="Reset"
                        click="textArea.text = template;" />
            </mx:FormItem>
        </mx:Form>
        <mx:TextArea id="textArea"
                text="{template}"
                editable="false"
                width="100%"
                height="100%" />
    </mx:HBox>

</mx:Application>

View source is enabled in the following example.


2 Responses to “Substituting values in strings using the Flex StringUtil class's substitute() method”


  1. 1 FlexLover Sep 8th, 2007 at 10:40 am

    Tank’you an other one very useful example!

  2. 2 whataslacker May 31st, 2008 at 10:18 pm

    this would make for a great ‘mad libs’ program

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".