trim First and Last spaces in string

Posted by druva | Flash, Flex, as3, utils | Wednesday 3 December 2008 2:30 am

This examples shows how to trim first and last spaces in string


 public function trim(str:String):String {
 if (str == null) { return ''; }
 return str.replace(/^\s+|\s+$/g, '');
 }

here is the complete example in flex –


<?xml version="1.0" encoding="utf-8"?>
<mx:Application
 xmlns:mx="http://www.adobe.com/2006/mxml"
 layout="absolute">
 <mx:Script>
 <![CDATA[
 public function trim(str:String):String {
 if (str == null) { return ''; }
 return str.replace(/^\s+|\s+$/g, '');
 }

 public function on_trim_button():void {
 reultText.text = trim(originalText.text);
 }
 ]]>
 </mx:Script>
 <mx:Button x="74" y="101" label="Trim Spaces" click="on_trim_button()"/>
 <mx:TextArea x="74" y="42" id="originalText" />
 <mx:TextArea x="74" y="157" id="reultText" />
 <mx:Label x="74" y="16" text="Enter Text" width="160"/>
 <mx:Label x="74" y="131" text="Enter Text" width="160"/>
</mx:Application>