<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flex Examples &#187; trimArrayElements()</title>
	<atom:link href="http://blog.flexexamples.com/tag/trimarrayelements/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Trimming strings using the Flex StringUtil class&#8217;s trimArrayElements() method</title>
		<link>http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trimarrayelements-method/</link>
		<comments>http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trimarrayelements-method/#comments</comments>
		<pubDate>Sat, 08 Sep 2007 06:01:09 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[StringUtil]]></category>
		<category><![CDATA[join()]]></category>
		<category><![CDATA[split()]]></category>
		<category><![CDATA[trimArrayElements()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trimarrayelements-method/</guid>
		<description><![CDATA[<p>The following example shows how you can remove leading and trailing spaces from an Array using the Flex StringUtil class&#8217;s trimArrayElements() method.</p> <p>Similar to my earlier post, &#8220;<a href="http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trim-method/">Trimming strings using the Flex StringUtil class&#8217;s trim() method</a>&#8220;, the StringUtil class has another rediculously handy method, trimArrayElements().</p> <p>Essentially, this method allows you to trim leading and [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can remove leading and trailing spaces from an Array using the Flex StringUtil class&#8217;s <code>trimArrayElements()</code> method.</p>
<p>Similar to my earlier post, &#8220;<a href="http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trim-method/">Trimming strings using the Flex StringUtil class&#8217;s trim() method</a>&#8220;, the StringUtil class has another rediculously handy method, <code>trimArrayElements()</code>.</p>
<p>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 &#8220;gotcha&#8221; is that this method expects a string as a parameter and return value instead of an Array object.</p>
<p>Sayeth the docs:</p>
<blockquote><p>
<code>public static function trimArrayElements(value:String, delimiter:String):String {...}</code><br />
Removes all whitespace characters from the beginning and end of each element in an Array, where the Array is stored as a String.
</p></blockquote>
<p>Full code after the jump.</p>
<p><span id="more-160"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/StringUtil_trimArrayElements_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trimarrayelements-method/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()"&gt;

    &lt;mx:Script&gt;
        &lt;![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() + "\'";
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Array id="arr"&gt;
        &lt;mx:String&gt;       The &lt;/mx:String&gt;
        &lt;mx:String&gt; quick    &lt;/mx:String&gt;
        &lt;mx:String&gt;
            brown

        &lt;/mx:String&gt;
        &lt;mx:String&gt;    fox                &lt;/mx:String&gt;
    &lt;/mx:Array&gt;

    &lt;mx:List id="list1"
            dataProvider="{arr}"
            variableRowHeight="true"
            labelFunction="addSingleQuotes" /&gt;

    &lt;mx:List id="list2"
            variableRowHeight="true"
            labelFunction="addSingleQuotes" /&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/StringUtil_trimArrayElements_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/StringUtil_trimArrayElements_test/bin/main.html" width="100%" height="220"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Trimming strings using the Flex StringUtil class\&#039;s trimArrayElements() method on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trimarrayelements-method/',contentID: 'post-160',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'join(),split(),trimArrayElements()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trimarrayelements-method/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Converting color names to numbers using the StyleManager class</title>
		<link>http://blog.flexexamples.com/2007/09/06/converting-color-names-to-numbers-using-the-stylemanager-class/</link>
		<comments>http://blog.flexexamples.com/2007/09/06/converting-color-names-to-numbers-using-the-stylemanager-class/#comments</comments>
		<pubDate>Fri, 07 Sep 2007 06:00:46 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[StringUtil]]></category>
		<category><![CDATA[StyleManager]]></category>
		<category><![CDATA[getColorName()]]></category>
		<category><![CDATA[getColorNames()]]></category>
		<category><![CDATA[trimArrayElements()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/09/06/converting-color-names-to-numbers-using-the-stylemanager-class/</guid>
		<description><![CDATA[<p>The following examples show how you can convert color names (such as &#8220;red&#8221;, &#8220;haloSilver&#8221;, &#8220;#FFFFFF&#8221;, and &#8220;0xFFFF00&#8243;) into numeric values, using the StyleManager class&#8217;s static getColorName() method and getColorNames() method.</p> <p>Full code after the jump.</p> <p></p> <p>The following example shows how you can convert a single color name into an integer using the static StyleManager.getColorName() [...]]]></description>
			<content:encoded><![CDATA[<p>The following examples show how you can convert color names (such as &#8220;red&#8221;, &#8220;haloSilver&#8221;, &#8220;#FFFFFF&#8221;, and &#8220;0xFFFF00&#8243;) into numeric values, using the StyleManager class&#8217;s static <code>getColorName()</code> method and <code>getColorNames()</code> method.</p>
<p>Full code after the jump.</p>
<p><span id="more-158"></span></p>
<p>The following example shows how you can convert a single color name into an integer using the static <code>StyleManager.getColorName()</code> method:</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorName_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/09/06/converting-color-names-to-numbers-using-the-stylemanager-class/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.styles.StyleManager;

            private function toHex(item:Object):String {
                var hex:String = Number(item).toString(16);
                return ("00000" + hex.toUpperCase()).substr(-6);
            }

            private function button_click():void {
                var colorInt:uint = StyleManager.getColorName(textInput.text);
                if (colorInt == StyleManager.NOT_A_COLOR) {
                    swatch.setStyle("backgroundColor", StyleManager.NOT_A_COLOR);
                    lbl.text = "NOT A COLOR";
                } else {
                    swatch.setStyle("backgroundColor", colorInt);
                    lbl.text = "#" + toHex(colorInt);
                }
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Label text="Color name:" /&gt;
        &lt;mx:TextInput id="textInput" /&gt;
        &lt;mx:Button id="button"
                label="Submit"
                click="button_click()" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:HBox&gt;
        &lt;mx:Box id="swatch"
                width="{lbl.height}"
                height="{lbl.height}" /&gt;

        &lt;mx:Label id="lbl"
                selectable="true"
                fontFamily="_typewriter"
                fontSize="64" /&gt;
    &lt;/mx:HBox&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorName_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorName_test/bin/main.html" width="100%" height="160"></iframe></p>
<p>The following example takes a comma separated list of color names and converts them into color values using the static <code>StyleManager.getColorName()</code> method within a data grid column&#8217;s label function:</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorName_test_2/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/09/06/converting-color-names-to-numbers-using-the-stylemanager-class/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.styles.StyleManager;
            import mx.utils.StringUtil;

            private const DELIM:String = ",";

            private function toHex(item:Object):String {
                var hex:String = Number(item).toString(16);
                return ("00000" + hex.toUpperCase()).substr(-6);
            }

            private function button_click():void {
                var colorStr:String = StringUtil.trimArrayElements(textInput.text, DELIM);
                dataGrid.dataProvider = colorStr.split(DELIM);
            }

            private function nameFunc(item:Object, column:DataGridColumn):String {
                return item.toString();
            }

            private function colorFunc(item:Object, column:DataGridColumn):String {
                var colorInt:uint = StyleManager.getColorName(item);
                if (colorInt == StyleManager.NOT_A_COLOR) {
                    return "NOT A COLOR";
                } else {
                    return "#" + toHex(colorInt);
                }
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Label text="Color names:" /&gt;
        &lt;mx:TextInput id="textInput" /&gt;
        &lt;mx:Button id="button"
                label="Submit"
                click="button_click()" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id="dataGrid" rowHeight="22"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn labelFunction="nameFunc"
                    headerText="Color name:" /&gt;
            &lt;mx:DataGridColumn labelFunction="colorFunc"
                    headerText="Color value:" /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorName_test_2/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorName_test_2/bin/main.html" width="100%" height="250"></iframe></p>
<p>Finally, the following example shows how you can convert an entire array of color names to integers at once using the static <code>StyleManager.getColorNames()</code> method:</p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorNames_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/09/06/converting-color-names-to-numbers-using-the-stylemanager-class/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.styles.StyleManager;
            import mx.utils.StringUtil;

            private const DELIM:String = ",";

            private function toHex(item:Object):String {
                var hex:String = Number(item).toString(16);
                return ("00000" + hex.toUpperCase()).substr(-6);
            }

            private function button_click():void {
                var colorStr:String = StringUtil.trimArrayElements(textInput.text, DELIM);
                var colorArr:Array = colorStr.split(DELIM);
                StyleManager.getColorNames(colorArr);

                dataGrid.dataProvider = colorArr;
            }

            private function nameFunc(item:Object, column:DataGridColumn):String {
                return item.toString();
            }

            private function colorFunc(item:Object, column:DataGridColumn):String {
                if (isNaN(Number(item))) {
                    return "NOT A COLOR";
                } else {
                    return "#" + toHex(item);
                }
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Label text="Color names:" /&gt;
        &lt;mx:TextInput id="textInput" /&gt;
        &lt;mx:Button id="button"
                label="Submit"
                click="button_click()" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:DataGrid id="dataGrid" rowHeight="22"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn labelFunction="nameFunc"
                    headerText="Color value (int):"
                    width="140" /&gt;
            &lt;mx:DataGridColumn labelFunction="colorFunc"
                    headerText="Color value (hex):"
                    width="140" /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorNames_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/StyleManager_getColorNames_test/bin/main.html" width="100%" height="250"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Converting color names to numbers using the StyleManager class on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/09/06/converting-color-names-to-numbers-using-the-stylemanager-class/',contentID: 'post-158',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'getColorName(),getColorNames(),trimArrayElements()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/09/06/converting-color-names-to-numbers-using-the-stylemanager-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

