mas01mj@732: /* mas01mj@732: Copyright (c) 2008, Adobe Systems Incorporated mas01mj@732: All rights reserved. mas01mj@732: mas01mj@732: Redistribution and use in source and binary forms, with or without mas01mj@732: modification, are permitted provided that the following conditions are mas01mj@732: met: mas01mj@732: mas01mj@732: * Redistributions of source code must retain the above copyright notice, mas01mj@732: this list of conditions and the following disclaimer. mas01mj@732: mas01mj@732: * Redistributions in binary form must reproduce the above copyright mas01mj@732: notice, this list of conditions and the following disclaimer in the mas01mj@732: documentation and/or other materials provided with the distribution. mas01mj@732: mas01mj@732: * Neither the name of Adobe Systems Incorporated nor the names of its mas01mj@732: contributors may be used to endorse or promote products derived from mas01mj@732: this software without specific prior written permission. mas01mj@732: mas01mj@732: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS mas01mj@732: IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, mas01mj@732: THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR mas01mj@732: PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR mas01mj@732: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, mas01mj@732: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, mas01mj@732: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR mas01mj@732: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF mas01mj@732: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING mas01mj@732: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS mas01mj@732: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. mas01mj@732: */ mas01mj@732: mas01mj@732: package com.adobe.utils mas01mj@732: { mas01mj@732: mas01mj@732: /** mas01mj@732: * Class that contains static utility methods for manipulating Strings. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public class StringUtil mas01mj@732: { mas01mj@732: mas01mj@732: mas01mj@732: /** mas01mj@732: * Does a case insensitive compare or two strings and returns true if mas01mj@732: * they are equal. mas01mj@732: * mas01mj@732: * @param s1 The first string to compare. mas01mj@732: * mas01mj@732: * @param s2 The second string to compare. mas01mj@732: * mas01mj@732: * @returns A boolean value indicating whether the strings' values are mas01mj@732: * equal in a case sensitive compare. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function stringsAreEqual(s1:String, s2:String, mas01mj@732: caseSensitive:Boolean):Boolean mas01mj@732: { mas01mj@732: if(caseSensitive) mas01mj@732: { mas01mj@732: return (s1 == s2); mas01mj@732: } mas01mj@732: else mas01mj@732: { mas01mj@732: return (s1.toUpperCase() == s2.toUpperCase()); mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Removes whitespace from the front and the end of the specified mas01mj@732: * string. mas01mj@732: * mas01mj@732: * @param input The String whose beginning and ending whitespace will mas01mj@732: * will be removed. mas01mj@732: * mas01mj@732: * @returns A String with whitespace removed from the begining and end mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function trim(input:String):String mas01mj@732: { mas01mj@732: return StringUtil.ltrim(StringUtil.rtrim(input)); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Removes whitespace from the front of the specified string. mas01mj@732: * mas01mj@732: * @param input The String whose beginning whitespace will will be removed. mas01mj@732: * mas01mj@732: * @returns A String with whitespace removed from the begining mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function ltrim(input:String):String mas01mj@732: { mas01mj@732: var size:Number = input.length; mas01mj@732: for(var i:Number = 0; i < size; i++) mas01mj@732: { mas01mj@732: if(input.charCodeAt(i) > 32) mas01mj@732: { mas01mj@732: return input.substring(i); mas01mj@732: } mas01mj@732: } mas01mj@732: return ""; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Removes whitespace from the end of the specified string. mas01mj@732: * mas01mj@732: * @param input The String whose ending whitespace will will be removed. mas01mj@732: * mas01mj@732: * @returns A String with whitespace removed from the end mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function rtrim(input:String):String mas01mj@732: { mas01mj@732: var size:Number = input.length; mas01mj@732: for(var i:Number = size; i > 0; i--) mas01mj@732: { mas01mj@732: if(input.charCodeAt(i - 1) > 32) mas01mj@732: { mas01mj@732: return input.substring(0, i); mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: return ""; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Determines whether the specified string begins with the spcified prefix. mas01mj@732: * mas01mj@732: * @param input The string that the prefix will be checked against. mas01mj@732: * mas01mj@732: * @param prefix The prefix that will be tested against the string. mas01mj@732: * mas01mj@732: * @returns True if the string starts with the prefix, false if it does not. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function beginsWith(input:String, prefix:String):Boolean mas01mj@732: { mas01mj@732: return (prefix == input.substring(0, prefix.length)); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Determines whether the specified string ends with the spcified suffix. mas01mj@732: * mas01mj@732: * @param input The string that the suffic will be checked against. mas01mj@732: * mas01mj@732: * @param prefix The suffic that will be tested against the string. mas01mj@732: * mas01mj@732: * @returns True if the string ends with the suffix, false if it does not. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function endsWith(input:String, suffix:String):Boolean mas01mj@732: { mas01mj@732: return (suffix == input.substring(input.length - suffix.length)); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Removes all instances of the remove string in the input string. mas01mj@732: * mas01mj@732: * @param input The string that will be checked for instances of remove mas01mj@732: * string mas01mj@732: * mas01mj@732: * @param remove The string that will be removed from the input string. mas01mj@732: * mas01mj@732: * @returns A String with the remove string removed. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function remove(input:String, remove:String):String mas01mj@732: { mas01mj@732: return StringUtil.replace(input, remove, ""); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Replaces all instances of the replace string in the input string mas01mj@732: * with the replaceWith string. mas01mj@732: * mas01mj@732: * @param input The string that instances of replace string will be mas01mj@732: * replaces with removeWith string. mas01mj@732: * mas01mj@732: * @param replace The string that will be replaced by instances of mas01mj@732: * the replaceWith string. mas01mj@732: * mas01mj@732: * @param replaceWith The string that will replace instances of replace mas01mj@732: * string. mas01mj@732: * mas01mj@732: * @returns A new String with the replace string replaced with the mas01mj@732: * replaceWith string. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function replace(input:String, replace:String, replaceWith:String):String mas01mj@732: { mas01mj@732: return input.split(replace).join(replaceWith); mas01mj@732: } mas01mj@732: mas01mj@732: mas01mj@732: /** mas01mj@732: * Specifies whether the specified string is either non-null, or contains mas01mj@732: * characters (i.e. length is greater that 0) mas01mj@732: * mas01mj@732: * @param s The string which is being checked for a value mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function stringHasValue(s:String):Boolean mas01mj@732: { mas01mj@732: //todo: this needs a unit test mas01mj@732: return (s != null && s.length > 0); mas01mj@732: } mas01mj@732: } mas01mj@732: }