The following example shows how you can remove leading and trailing spaces from an Array using the Flex StringUtil class’s trimArrayElements() method.

Similar to my earlier post, “Trimming strings using the Flex StringUtil class’s trim() method“, the StringUtil class has another rediculously handy method, trimArrayElements().

Essentially, this method allows you to trim leading and trailing whitespace from every element an array with one single method call, rather than having to resort to awkward loops and variable reassignments. The only real “gotcha” is that this method expects a string as a parameter and return value instead of an Array object.

Sayeth the docs:

public static function trimArrayElements(value:String, delimiter:String):String {...}
Removes all whitespace characters from the beginning and end of each element in an Array, where the Array is stored as a String.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trimarrayelements-method/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()">

    <mx:Script>
        <![CDATA[
            import mx.utils.StringUtil;

            private function init():void {
                var arrayStr:String = arrayToList(arr);
                arrayStr = StringUtil.trimArrayElements(arrayStr, ",");
                list2.dataProvider = listToArray(arrayStr);
            }

            private function listToArray(value:String, delimiter:String = ","):Array {
                return value.split(delimiter);
            }

            private function arrayToList(value:Array, delimiter:String = ","):String {
                return value.join(delimiter);
            }

            private function addSingleQuotes(item:Object):String {
                return "\'" + item.toString() + "\'";
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:String>       The </mx:String>
        <mx:String> quick    </mx:String>
        <mx:String>
            brown

        </mx:String>
        <mx:String>    fox                </mx:String>
    </mx:Array>

    <mx:List id="list1"
            dataProvider="{arr}"
            variableRowHeight="true"
            labelFunction="addSingleQuotes" />

    <mx:List id="list2"
            variableRowHeight="true"
            labelFunction="addSingleQuotes" />

</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

3 Responses to Trimming strings using the Flex StringUtil class’s trimArrayElements() method

  1. gregory says:

    this blog is very helpful, and more easily found via google, thanks for existing.

  2. amutha says:

    This is nice one… but i want to split and join the image…could you please tell me how to do it in flex 3

  3. hardik says:

    thanks a ton..

    all of your posts are very helpful.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree