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

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>
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 Follow us Follow us
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