Flex中利用StringUtil类的substitute()方法对字符串中的字符进行替换的例子
By Minidxer | August 24, 2008
很多系统中当用户注册或者作了某项操作的时候,系统会自动发送邮件或者消息给该用户,其中大部分的内容是一样的,不过比如用户名,用户的操作之类的则是不一样的。接下来的例子就演示了Flex中如何利用StringUtil类的substitute()方法,对字符串中的字符进行替换。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <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>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Flex |
No Comments » |
Tags: StringUtil, substitute