2   Copyright (c) 2008, Adobe Systems Incorporated
 
   5   Redistribution and use in source and binary forms, with or without 
 
   6   modification, are permitted provided that the following conditions are
 
   9   * Redistributions of source code must retain the above copyright notice, 
 
  10     this list of conditions and the following disclaimer.
 
  12   * Redistributions in binary form must reproduce the above copyright
 
  13     notice, this list of conditions and the following disclaimer in the 
 
  14     documentation and/or other materials provided with the distribution.
 
  16   * Neither the name of Adobe Systems Incorporated nor the names of its 
 
  17     contributors may be used to endorse or promote products derived from 
 
  18     this software without specific prior written permission.
 
  20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 
  21   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
  22   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
  23   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 
  24   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
  25   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
  26   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
  27   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
  28   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
  29   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
  30   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  33 package com.adobe.utils
 
  37         *       Class that contains static utility methods for manipulating Strings.
 
  39         *       @langversion ActionScript 3.0
 
  40         *       @playerversion Flash 9.0
 
  43         public class StringUtil
 
  48                 *       Does a case insensitive compare or two strings and returns true if
 
  51                 *       @param s1 The first string to compare.
 
  53                 *       @param s2 The second string to compare.
 
  55                 *       @returns A boolean value indicating whether the strings' values are 
 
  56                 *       equal in a case sensitive compare.      
 
  58                 *       @langversion ActionScript 3.0
 
  59                 *       @playerversion Flash 9.0
 
  62                 public static function stringsAreEqual(s1:String, s2:String, 
 
  63                                                                                         caseSensitive:Boolean):Boolean
 
  71                                 return (s1.toUpperCase() == s2.toUpperCase());
 
  76                 *       Removes whitespace from the front and the end of the specified
 
  79                 *       @param input The String whose beginning and ending whitespace will
 
  82                 *       @returns A String with whitespace removed from the begining and end     
 
  84                 *       @langversion ActionScript 3.0
 
  85                 *       @playerversion Flash 9.0
 
  88                 public static function trim(input:String):String
 
  90                         return StringUtil.ltrim(StringUtil.rtrim(input));
 
  94                 *       Removes whitespace from the front of the specified string.
 
  96                 *       @param input The String whose beginning whitespace will will be removed.
 
  98                 *       @returns A String with whitespace removed from the begining     
 
 100                 *       @langversion ActionScript 3.0
 
 101                 *       @playerversion Flash 9.0
 
 104                 public static function ltrim(input:String):String
 
 106                         var size:Number = input.length;
 
 107                         for(var i:Number = 0; i < size; i++)
 
 109                                 if(input.charCodeAt(i) > 32)
 
 111                                         return input.substring(i);
 
 118                 *       Removes whitespace from the end of the specified string.
 
 120                 *       @param input The String whose ending whitespace will will be removed.
 
 122                 *       @returns A String with whitespace removed from the end  
 
 124                 *       @langversion ActionScript 3.0
 
 125                 *       @playerversion Flash 9.0
 
 128                 public static function rtrim(input:String):String
 
 130                         var size:Number = input.length;
 
 131                         for(var i:Number = size; i > 0; i--)
 
 133                                 if(input.charCodeAt(i - 1) > 32)
 
 135                                         return input.substring(0, i);
 
 143                 *       Determines whether the specified string begins with the spcified prefix.
 
 145                 *       @param input The string that the prefix will be checked against.
 
 147                 *       @param prefix The prefix that will be tested against the string.
 
 149                 *       @returns True if the string starts with the prefix, false if it does not.
 
 151                 *       @langversion ActionScript 3.0
 
 152                 *       @playerversion Flash 9.0
 
 155                 public static function beginsWith(input:String, prefix:String):Boolean
 
 157                         return (prefix == input.substring(0, prefix.length));
 
 161                 *       Determines whether the specified string ends with the spcified suffix.
 
 163                 *       @param input The string that the suffic will be checked against.
 
 165                 *       @param prefix The suffic that will be tested against the string.
 
 167                 *       @returns True if the string ends with the suffix, false if it does not.
 
 169                 *       @langversion ActionScript 3.0
 
 170                 *       @playerversion Flash 9.0
 
 173                 public static function endsWith(input:String, suffix:String):Boolean
 
 175                         return (suffix == input.substring(input.length - suffix.length));
 
 179                 *       Removes all instances of the remove string in the input string.
 
 181                 *       @param input The string that will be checked for instances of remove
 
 184                 *       @param remove The string that will be removed from the input string.
 
 186                 *       @returns A String with the remove string removed.
 
 188                 *       @langversion ActionScript 3.0
 
 189                 *       @playerversion Flash 9.0
 
 192                 public static function remove(input:String, remove:String):String
 
 194                         return StringUtil.replace(input, remove, "");
 
 198                 *       Replaces all instances of the replace string in the input string
 
 199                 *       with the replaceWith string.
 
 201                 *       @param input The string that instances of replace string will be 
 
 202                 *       replaces with removeWith string.
 
 204                 *       @param replace The string that will be replaced by instances of 
 
 205                 *       the replaceWith string.
 
 207                 *       @param replaceWith The string that will replace instances of replace
 
 210                 *       @returns A new String with the replace string replaced with the 
 
 211                 *       replaceWith string.
 
 213                 *       @langversion ActionScript 3.0
 
 214                 *       @playerversion Flash 9.0
 
 217                 public static function replace(input:String, replace:String, replaceWith:String):String
 
 219                         //change to StringBuilder
 
 220                         var sb:String = new String();
 
 221                         var found:Boolean = false;
 
 223                         var sLen:Number = input.length;
 
 224                         var rLen:Number = replace.length;
 
 226                         for (var i:Number = 0; i < sLen; i++)
 
 228                                 if(input.charAt(i) == replace.charAt(0))
 
 231                                         for(var j:Number = 0; j < rLen; j++)
 
 233                                                 if(!(input.charAt(i + j) == replace.charAt(j)))
 
 247                                 sb += input.charAt(i);
 
 249                         //TODO : if the string is not found, should we return the original
 
 255                 *       Specifies whether the specified string is either non-null, or contains
 
 256                 *       characters (i.e. length is greater that 0)
 
 258                 *       @param s The string which is being checked for a value
 
 260                 *       @langversion ActionScript 3.0
 
 261                 *       @playerversion Flash 9.0
 
 264                 public static function stringHasValue(s:String):Boolean
 
 266                         //todo: this needs a unit test
 
 267                         return (s != null && s.length > 0);