[7849] | 1 | /*
|
---|
| 2 | Adobe Systems Incorporated(r) Source Code License Agreement
|
---|
| 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
---|
| 4 |
|
---|
| 5 | Please read this Source Code License Agreement carefully before using
|
---|
| 6 | the source code.
|
---|
| 7 |
|
---|
| 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
|
---|
| 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce,
|
---|
| 10 | prepare derivative works of, publicly display, publicly perform, and
|
---|
| 11 | distribute this source code and such derivative works in source or
|
---|
| 12 | object code form without any attribution requirements.
|
---|
| 13 |
|
---|
| 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products
|
---|
| 15 | derived from the source code without prior written permission.
|
---|
| 16 |
|
---|
| 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
|
---|
| 18 | against any loss, damage, claims or lawsuits, including attorney's
|
---|
| 19 | fees that arise or result from your use or distribution of the source
|
---|
| 20 | code.
|
---|
| 21 |
|
---|
| 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
|
---|
| 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
---|
| 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
| 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
|
---|
| 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
|
---|
| 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
---|
| 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
---|
| 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
---|
| 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
|
---|
| 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | package com.adobe.utils
|
---|
| 37 | {
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Class that contains static utility methods for manipulating Strings.
|
---|
| 41 | *
|
---|
| 42 | * @langversion ActionScript 3.0
|
---|
| 43 | * @playerversion Flash 9.0
|
---|
| 44 | * @tiptext
|
---|
| 45 | */
|
---|
| 46 | public class StringUtil
|
---|
| 47 | {
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Does a case insensitive compare or two strings and returns true if
|
---|
| 52 | * they are equal.
|
---|
| 53 | *
|
---|
| 54 | * @param s1 The first string to compare.
|
---|
| 55 | *
|
---|
| 56 | * @param s2 The second string to compare.
|
---|
| 57 | *
|
---|
| 58 | * @returns A boolean value indicating whether the strings' values are
|
---|
| 59 | * equal in a case sensitive compare.
|
---|
| 60 | *
|
---|
| 61 | * @langversion ActionScript 3.0
|
---|
| 62 | * @playerversion Flash 9.0
|
---|
| 63 | * @tiptext
|
---|
| 64 | */
|
---|
| 65 | public static function stringsAreEqual(s1:String, s2:String,
|
---|
| 66 | caseSensitive:Boolean):Boolean
|
---|
| 67 | {
|
---|
| 68 | if(caseSensitive)
|
---|
| 69 | {
|
---|
| 70 | return (s1 == s2);
|
---|
| 71 | }
|
---|
| 72 | else
|
---|
| 73 | {
|
---|
| 74 | return (s1.toUpperCase() == s2.toUpperCase());
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Removes whitespace from the front and the end of the specified
|
---|
| 80 | * string.
|
---|
| 81 | *
|
---|
| 82 | * @param input The String whose beginning and ending whitespace will
|
---|
| 83 | * will be removed.
|
---|
| 84 | *
|
---|
| 85 | * @returns A String with whitespace removed from the begining and end
|
---|
| 86 | *
|
---|
| 87 | * @langversion ActionScript 3.0
|
---|
| 88 | * @playerversion Flash 9.0
|
---|
| 89 | * @tiptext
|
---|
| 90 | */
|
---|
| 91 | public static function trim(input:String):String
|
---|
| 92 | {
|
---|
| 93 | return StringUtil.ltrim(StringUtil.rtrim(input));
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * Removes whitespace from the front of the specified string.
|
---|
| 98 | *
|
---|
| 99 | * @param input The String whose beginning whitespace will will be removed.
|
---|
| 100 | *
|
---|
| 101 | * @returns A String with whitespace removed from the begining
|
---|
| 102 | *
|
---|
| 103 | * @langversion ActionScript 3.0
|
---|
| 104 | * @playerversion Flash 9.0
|
---|
| 105 | * @tiptext
|
---|
| 106 | */
|
---|
| 107 | public static function ltrim(input:String):String
|
---|
| 108 | {
|
---|
| 109 | var size:Number = input.length;
|
---|
| 110 | for(var i:Number = 0; i < size; i++)
|
---|
| 111 | {
|
---|
| 112 | if(input.charCodeAt(i) > 32)
|
---|
| 113 | {
|
---|
| 114 | return input.substring(i);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | return "";
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Removes whitespace from the end of the specified string.
|
---|
| 122 | *
|
---|
| 123 | * @param input The String whose ending whitespace will will be removed.
|
---|
| 124 | *
|
---|
| 125 | * @returns A String with whitespace removed from the end
|
---|
| 126 | *
|
---|
| 127 | * @langversion ActionScript 3.0
|
---|
| 128 | * @playerversion Flash 9.0
|
---|
| 129 | * @tiptext
|
---|
| 130 | */
|
---|
| 131 | public static function rtrim(input:String):String
|
---|
| 132 | {
|
---|
| 133 | var size:Number = input.length;
|
---|
| 134 | for(var i:Number = size; i > 0; i--)
|
---|
| 135 | {
|
---|
| 136 | if(input.charCodeAt(i - 1) > 32)
|
---|
| 137 | {
|
---|
| 138 | return input.substring(0, i);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | return "";
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /**
|
---|
| 146 | * Determines whether the specified string begins with the spcified prefix.
|
---|
| 147 | *
|
---|
| 148 | * @param input The string that the prefix will be checked against.
|
---|
| 149 | *
|
---|
| 150 | * @param prefix The prefix that will be tested against the string.
|
---|
| 151 | *
|
---|
| 152 | * @returns True if the string starts with the prefix, false if it does not.
|
---|
| 153 | *
|
---|
| 154 | * @langversion ActionScript 3.0
|
---|
| 155 | * @playerversion Flash 9.0
|
---|
| 156 | * @tiptext
|
---|
| 157 | */
|
---|
| 158 | public static function beginsWith(input:String, prefix:String):Boolean
|
---|
| 159 | {
|
---|
| 160 | return (prefix == input.substring(0, prefix.length));
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Determines whether the specified string ends with the spcified suffix.
|
---|
| 165 | *
|
---|
| 166 | * @param input The string that the suffic will be checked against.
|
---|
| 167 | *
|
---|
| 168 | * @param prefix The suffic that will be tested against the string.
|
---|
| 169 | *
|
---|
| 170 | * @returns True if the string ends with the suffix, false if it does not.
|
---|
| 171 | *
|
---|
| 172 | * @langversion ActionScript 3.0
|
---|
| 173 | * @playerversion Flash 9.0
|
---|
| 174 | * @tiptext
|
---|
| 175 | */
|
---|
| 176 | public static function endsWith(input:String, suffix:String):Boolean
|
---|
| 177 | {
|
---|
| 178 | return (suffix == input.substring(input.length - suffix.length));
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * Removes all instances of the remove string in the input string.
|
---|
| 183 | *
|
---|
| 184 | * @param input The string that will be checked for instances of remove
|
---|
| 185 | * string
|
---|
| 186 | *
|
---|
| 187 | * @param remove The string that will be removed from the input string.
|
---|
| 188 | *
|
---|
| 189 | * @returns A String with the remove string removed.
|
---|
| 190 | *
|
---|
| 191 | * @langversion ActionScript 3.0
|
---|
| 192 | * @playerversion Flash 9.0
|
---|
| 193 | * @tiptext
|
---|
| 194 | */
|
---|
| 195 | public static function remove(input:String, remove:String):String
|
---|
| 196 | {
|
---|
| 197 | return StringUtil.replace(input, remove, "");
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /**
|
---|
| 201 | * Replaces all instances of the replace string in the input string
|
---|
| 202 | * with the replaceWith string.
|
---|
| 203 | *
|
---|
| 204 | * @param input The string that instances of replace string will be
|
---|
| 205 | * replaces with removeWith string.
|
---|
| 206 | *
|
---|
| 207 | * @param replace The string that will be replaced by instances of
|
---|
| 208 | * the replaceWith string.
|
---|
| 209 | *
|
---|
| 210 | * @param replaceWith The string that will replace instances of replace
|
---|
| 211 | * string.
|
---|
| 212 | *
|
---|
| 213 | * @returns A new String with the replace string replaced with the
|
---|
| 214 | * replaceWith string.
|
---|
| 215 | *
|
---|
| 216 | * @langversion ActionScript 3.0
|
---|
| 217 | * @playerversion Flash 9.0
|
---|
| 218 | * @tiptext
|
---|
| 219 | */
|
---|
| 220 | public static function replace(input:String, replace:String, replaceWith:String):String
|
---|
| 221 | {
|
---|
| 222 | //change to StringBuilder
|
---|
| 223 | var sb:String = new String();
|
---|
| 224 | var found:Boolean = false;
|
---|
| 225 |
|
---|
| 226 | var sLen:Number = input.length;
|
---|
| 227 | var rLen:Number = replace.length;
|
---|
| 228 |
|
---|
| 229 | for (var i:Number = 0; i < sLen; i++)
|
---|
| 230 | {
|
---|
| 231 | if(input.charAt(i) == replace.charAt(0))
|
---|
| 232 | {
|
---|
| 233 | found = true;
|
---|
| 234 | for(var j:Number = 0; j < rLen; j++)
|
---|
| 235 | {
|
---|
| 236 | if(!(input.charAt(i + j) == replace.charAt(j)))
|
---|
| 237 | {
|
---|
| 238 | found = false;
|
---|
| 239 | break;
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | if(found)
|
---|
| 244 | {
|
---|
| 245 | sb += replaceWith;
|
---|
| 246 | i = i + (rLen - 1);
|
---|
| 247 | continue;
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | sb += input.charAt(i);
|
---|
| 251 | }
|
---|
| 252 | //TODO : if the string is not found, should we return the original
|
---|
| 253 | //string?
|
---|
| 254 | return sb;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | }
|
---|