StringUtil class contains static utility methods for manipulating Strings. It simplifies the way to handle string values to meet the faults of StringUtil defined in package mx.util. For example, I want to remove all the string “mx:” in the StringUtilDemo.mxml. Just call the method like StringUtil.remove(theContent,”mx:”). However, the method is intrinsic case sensitive. So if I call the method like StringUtil.remove(theContent,”MX:”), it doesn’t make sense. The same as the method “replace”.
Screenshot:
Input original string here.
Test method beginsWith.
Methods:
1. beginsWith()
public static function beginsWith(input:String, prefix:String):Boolean
Determines whether the specified string begins with the specified prefix.
Parameters:
input:String The string that the prefix will be checked against.
prefix:String The prefix that will be tested against the string.
Returns:Boolean
2. endsWith()
public static function endsWith(input:String, suffix:String):Boolean
Determines whether the specified string ends with the spcified suffix.
Parameters:
input:String The string that the suffic will be checked against.
suffix:String The suffic that will be tested against the string.
Returns:Boolean
3. ltrim()
public static function ltrim(input:String):String
Removes whitespace from the front of the specified string.
Parameters:
input:String The String whose beginning whitespace will will be removed.
Returns:String
4. remove()
public static function remove(input:String, remove:String):String
Removes all instances of the remove string in the input string.
Parameters:
input:String The string that will be checked for instances of remove string.
remove:String The string that will be removed from the input string.
Returns:String
5. replace()
public static function replace(input:String, replace:String, replaceWith:String):String
Replaces all instances of the replace string in the input string with the replaceWith string.
Parameters:
input:String The string that instances of replace string will be replaces with removeWith string.
replace:String The string that will be replaced by instances of the replaceWith string.
replaceWith:String The string that will replace instances of replace string.
Returns:String
6. rtrim()
public static function rtrim(input:String):String
Removes whitespace from the end of the specified string.
Parameters:
input:String The String whose ending whitespace will will be removed.
Returns:String
7. stringsAreEqual()
public static function stringsAreEqual(s1:String, s2:String, caseSensitive:Boolean):Boolean
Does a case insensitive compare or two strings and returns true if they are equal.
Parameters:
s1:String The first string to compare.
s2:String The second string to compare.
caseSensitive:Boolean
Returns:Boolean
8. trim()
public static function trim(input:String):String
Removes whitespace from the front and the end of the specified string.
Parameters:
input:String The String whose beginning and ending whitespace will will be removed.
Returns:String
The following is full source code of NumberFormatter StringUtilDemo.mxml:
<?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:Text text="beginsWith:{ StringUtil.beginsWith(input1.text,beginsWith.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>

September 9th, 2008
Ntt.cc
Posted in
Tags: 
RSS Feed
Email Feed
[...] the Package com.adobe.utils, include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]