Flex StringUtil工具类

最近发现Flex处理字符串的工具类一共有三个,分别是:

  1. mx.utils包下的StringUtil
  2. mx.utils包下的RPCStringUtil
  3. as3corelib类库下的StringUtil

下面分别整理及介绍下这三个工具类的用法:

mx.utils包下的StringUtil

1.trim()

2.isWhitespace()   判断是否是空白字符

3.trimArrayElements()

删除数组中每个元素的开头和末尾的所有空格字符,此处数组作为字符串存储。

4.substitute()

使用传入的各个参数替换指定的字符串内的“{n}”标记。

示例:

var str:String = "here is some info '{0}' and {1}"; 

trace(StringUtil.substitute(str, 15.4, true)); 

// this will output the following string: 

// "here is some info '15.4' and true" 


mx.utils包下的RPCStringUtil

mx.utils下包StringUtil的子集,用法同StringUtil

as3corelib类库下的StringUtil
  1. stringsAreEqual()    判断字符串是否相等
  2. trim()    去除首尾的空白字符
  3. ltrim()   去除首部的空白字符
  4. rtrim()   去除尾部的空白字符
  5. beginsWith()   判断字符串是否已该前缀开头
  6. endsWith()    判断字符串是否已该后缀结尾
  7. remove()    去除字符串中的部分字符
  8. replace()   替换字符串中的部分字符
  9. stringHasValue()  判断字符串是否有值
代码示例:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	backgroundColor="#ffffff"
	layout="absolute">
	<mx:Script>
		<![CDATA[
			import com.adobe.utils.StringUtil;
		]]>
	</mx:Script>
	<mx:VBox>
		<mx:HBox>
			<mx:Label text="input1"/>		
			<mx:TextInput id="input1" text="input1"/>
		</mx:HBox>
		<mx:HRule width="100%" height="1"/>	
		<mx:HBox>
			<mx:Label text="is input1 begins with:"/>		
			<mx:TextInput id="beginsWith" text="i"/>
		</mx:HBox>	
		<mx:HRule width="100%" height="1"/>	
		<mx:Text text="beginsWith:{ StringUtil.beginsWith(input1.text,beginsWith.text)}"/>
		<mx:HRule width="100%" height="1"/>
		<mx:Text text="input1 has value ?:{ StringUtil.stringHasValue(input1.text) }"/>
		<mx:HRule width="100%" height="1"/>
		<mx:HBox>
			<mx:Label text="is input1 ends with:"/>		
			<mx:TextInput id="endsWith" text="1"/>
		</mx:HBox>
		<mx:Text text="endsWith:{ StringUtil.endsWith(input1.text,endsWith.text)}"/>			
		<mx:HRule width="100%" height="1"/>
		<mx:HBox>
			<mx:Label text="string to be removed from input1"/>		
			<mx:TextInput id="removeString"/>
		</mx:HBox>
		<mx:Text text="remove:{ StringUtil.remove(input1.text,removeString.text)}"/>
		<mx:HRule width="100%" height="1"/>
		<mx:HBox>
			<mx:Label text="string to be replaced from input1"/>		
			<mx:TextInput id="replace"/>
		</mx:HBox>
		<mx:HBox>
			<mx:Label text="string to be replace with"/>		
			<mx:TextInput id="replaceWith"/>
		</mx:HBox>			
		<mx:Text text="replace:{ StringUtil.replace(input1.text,replace.text,replaceWith.text) }"/>
		<mx:HRule width="100%" height="1"/>
		<mx:Text text="ltrim:{ StringUtil.ltrim(input1.text)}"/>
		<mx:Text text="trim:{StringUtil.trim(input1.text)}"/>
		<mx:Text text="rtrim:{StringUtil.rtrim(input1.text)}"/>
		<mx:HRule width="100%" height="1"/>
		<mx:HBox>
			<mx:Label text="input2"/>		
			<mx:TextInput id="input2" text="input1"/>
			<mx:Label text="is case sensitive?"/><mx:CheckBox id="caseSensitive" selected="true"/>
		</mx:HBox>
		<mx:Text text="input1 and input2 are equal?:{ StringUtil.stringsAreEqual(input1.text,input2.text,caseSensitive.selected)}"/>
	</mx:VBox>
</mx:Application>


输出结果:

2011-07-29_1552

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值