as3corelib Tutorial:How to Use StringUtil Class in Flex

papa-smurf-256x256 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”.

Search-256x256 Demo | DownloadDownload Full Project

Screenshot:

StringUtil-01

Input original string here.

StringUtil-02

Test method beginsWith.

StringUtil-03

Methods:

1. beginsWith()

  1. 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()

  1. 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()

  1. 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()

  1. 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()

  1. 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()

  1. 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()

  1. 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()

  1. 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:

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application 
  3.     xmlns:mx="http://www.adobe.com/2006/mxml" 
  4.     backgroundColor="#ffffff"
  5.     layout="absolute">
  6.     <mx:Script>
  7.         <![CDATA[
  8.             import com.adobe.utils.StringUtil;
  9.         ]]>
  10.     </mx:Script>
  11.     <mx:VBox>
  12.         <mx:HBox>
  13.             <mx:Label text="input1"/>       
  14.             <mx:TextInput id="input1" text="input1"/>
  15.         </mx:HBox>
  16.        
  17.         <mx:HRule width="100%" height="1"/>
  18.        
  19.         <mx:HBox>
  20.             <mx:Label text="is input1 begins with:"/>       
  21.             <mx:TextInput id="beginsWith" text="i"/>
  22.         </mx:HBox>
  23.         <mx:Text text="beginsWith:{ StringUtil.beginsWith(input1.text,beginsWith.text)}"/>
  24.        
  25.         <mx:HRule width="100%" height="1"/>
  26.        
  27.         <mx:HBox>
  28.             <mx:Label text="is input1 ends with:"/>       
  29.             <mx:TextInput id="endsWith" text="1"/>
  30.         </mx:HBox>
  31.         <mx:Text text="endsWith:{ StringUtil.endsWith(input1.text,endsWith.text)}"/>       
  32.        
  33.         <mx:HRule width="100%" height="1"/>
  34.        
  35.         <mx:HBox>
  36.             <mx:Label text="string to be removed from input1"/>       
  37.             <mx:TextInput id="removeString"/>
  38.         </mx:HBox>
  39.         <mx:Text text="remove:{ StringUtil.remove(input1.text,removeString.text)}"/>
  40.        
  41.         <mx:HRule width="100%" height="1"/>
  42.        
  43.         <mx:HBox>
  44.             <mx:Label text="string to be replaced from input1"/>       
  45.             <mx:TextInput id="replace"/>
  46.         </mx:HBox>
  47.        
  48.         <mx:HBox>
  49.             <mx:Label text="string to be replace with"/>       
  50.             <mx:TextInput id="replaceWith"/>
  51.         </mx:HBox>
  52.                    
  53.         <mx:Text text="replace:{ StringUtil.replace(input1.text,replace.text,replaceWith.text) }"/>
  54.        
  55.         <mx:HRule width="100%" height="1"/>
  56.        
  57.         <mx:Text text="ltrim:{ StringUtil.ltrim(input1.text)}"/>
  58.         <mx:Text text="trim:{StringUtil.trim(input1.text)}"/>
  59.         <mx:Text text="rtrim:{StringUtil.rtrim(input1.text)}"/>
  60.        
  61.         <mx:HRule width="100%" height="1"/>
  62.        
  63.         <mx:HBox>
  64.             <mx:Label text="input2"/>       
  65.             <mx:TextInput id="input2" text="input1"/>
  66.             <mx:Label text="is case sensitive?"/><mx:CheckBox id="caseSensitive" selected="true"/>
  67.         </mx:HBox>
  68.         <mx:Text text="input1 and input2 are equal?:{ StringUtil.stringsAreEqual(input1.text,input2.text,caseSensitive.selected)}"/>
  69.  
  70.     </mx:VBox>
  71. </mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed
You can leave a response, or trackback from your own site.

One Response to “as3corelib Tutorial:How to Use StringUtil Class in Flex”

  1. [...] the Package com.adobe.utils, include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]

Leave a Reply