mas01mj@732
|
1 /*
|
mas01mj@732
|
2 Copyright (c) 2008, Adobe Systems Incorporated
|
mas01mj@732
|
3 All rights reserved.
|
mas01mj@732
|
4
|
mas01mj@732
|
5 Redistribution and use in source and binary forms, with or without
|
mas01mj@732
|
6 modification, are permitted provided that the following conditions are
|
mas01mj@732
|
7 met:
|
mas01mj@732
|
8
|
mas01mj@732
|
9 * Redistributions of source code must retain the above copyright notice,
|
mas01mj@732
|
10 this list of conditions and the following disclaimer.
|
mas01mj@732
|
11
|
mas01mj@732
|
12 * Redistributions in binary form must reproduce the above copyright
|
mas01mj@732
|
13 notice, this list of conditions and the following disclaimer in the
|
mas01mj@732
|
14 documentation and/or other materials provided with the distribution.
|
mas01mj@732
|
15
|
mas01mj@732
|
16 * Neither the name of Adobe Systems Incorporated nor the names of its
|
mas01mj@732
|
17 contributors may be used to endorse or promote products derived from
|
mas01mj@732
|
18 this software without specific prior written permission.
|
mas01mj@732
|
19
|
mas01mj@732
|
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
mas01mj@732
|
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
mas01mj@732
|
23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
mas01mj@732
|
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
mas01mj@732
|
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
mas01mj@732
|
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
mas01mj@732
|
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
mas01mj@732
|
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
mas01mj@732
|
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
mas01mj@732
|
31 */
|
mas01mj@732
|
32
|
mas01mj@732
|
33 package com.adobe.utils
|
mas01mj@732
|
34 {
|
mas01mj@732
|
35
|
mas01mj@732
|
36 /**
|
mas01mj@732
|
37 * Class that contains static utility methods for manipulating Strings.
|
mas01mj@732
|
38 *
|
mas01mj@732
|
39 * @langversion ActionScript 3.0
|
mas01mj@732
|
40 * @playerversion Flash 9.0
|
mas01mj@732
|
41 * @tiptext
|
mas01mj@732
|
42 */
|
mas01mj@732
|
43 public class StringUtil
|
mas01mj@732
|
44 {
|
mas01mj@732
|
45
|
mas01mj@732
|
46
|
mas01mj@732
|
47 /**
|
mas01mj@732
|
48 * Does a case insensitive compare or two strings and returns true if
|
mas01mj@732
|
49 * they are equal.
|
mas01mj@732
|
50 *
|
mas01mj@732
|
51 * @param s1 The first string to compare.
|
mas01mj@732
|
52 *
|
mas01mj@732
|
53 * @param s2 The second string to compare.
|
mas01mj@732
|
54 *
|
mas01mj@732
|
55 * @returns A boolean value indicating whether the strings' values are
|
mas01mj@732
|
56 * equal in a case sensitive compare.
|
mas01mj@732
|
57 *
|
mas01mj@732
|
58 * @langversion ActionScript 3.0
|
mas01mj@732
|
59 * @playerversion Flash 9.0
|
mas01mj@732
|
60 * @tiptext
|
mas01mj@732
|
61 */
|
mas01mj@732
|
62 public static function stringsAreEqual(s1:String, s2:String,
|
mas01mj@732
|
63 caseSensitive:Boolean):Boolean
|
mas01mj@732
|
64 {
|
mas01mj@732
|
65 if(caseSensitive)
|
mas01mj@732
|
66 {
|
mas01mj@732
|
67 return (s1 == s2);
|
mas01mj@732
|
68 }
|
mas01mj@732
|
69 else
|
mas01mj@732
|
70 {
|
mas01mj@732
|
71 return (s1.toUpperCase() == s2.toUpperCase());
|
mas01mj@732
|
72 }
|
mas01mj@732
|
73 }
|
mas01mj@732
|
74
|
mas01mj@732
|
75 /**
|
mas01mj@732
|
76 * Removes whitespace from the front and the end of the specified
|
mas01mj@732
|
77 * string.
|
mas01mj@732
|
78 *
|
mas01mj@732
|
79 * @param input The String whose beginning and ending whitespace will
|
mas01mj@732
|
80 * will be removed.
|
mas01mj@732
|
81 *
|
mas01mj@732
|
82 * @returns A String with whitespace removed from the begining and end
|
mas01mj@732
|
83 *
|
mas01mj@732
|
84 * @langversion ActionScript 3.0
|
mas01mj@732
|
85 * @playerversion Flash 9.0
|
mas01mj@732
|
86 * @tiptext
|
mas01mj@732
|
87 */
|
mas01mj@732
|
88 public static function trim(input:String):String
|
mas01mj@732
|
89 {
|
mas01mj@732
|
90 return StringUtil.ltrim(StringUtil.rtrim(input));
|
mas01mj@732
|
91 }
|
mas01mj@732
|
92
|
mas01mj@732
|
93 /**
|
mas01mj@732
|
94 * Removes whitespace from the front of the specified string.
|
mas01mj@732
|
95 *
|
mas01mj@732
|
96 * @param input The String whose beginning whitespace will will be removed.
|
mas01mj@732
|
97 *
|
mas01mj@732
|
98 * @returns A String with whitespace removed from the begining
|
mas01mj@732
|
99 *
|
mas01mj@732
|
100 * @langversion ActionScript 3.0
|
mas01mj@732
|
101 * @playerversion Flash 9.0
|
mas01mj@732
|
102 * @tiptext
|
mas01mj@732
|
103 */
|
mas01mj@732
|
104 public static function ltrim(input:String):String
|
mas01mj@732
|
105 {
|
mas01mj@732
|
106 var size:Number = input.length;
|
mas01mj@732
|
107 for(var i:Number = 0; i < size; i++)
|
mas01mj@732
|
108 {
|
mas01mj@732
|
109 if(input.charCodeAt(i) > 32)
|
mas01mj@732
|
110 {
|
mas01mj@732
|
111 return input.substring(i);
|
mas01mj@732
|
112 }
|
mas01mj@732
|
113 }
|
mas01mj@732
|
114 return "";
|
mas01mj@732
|
115 }
|
mas01mj@732
|
116
|
mas01mj@732
|
117 /**
|
mas01mj@732
|
118 * Removes whitespace from the end of the specified string.
|
mas01mj@732
|
119 *
|
mas01mj@732
|
120 * @param input The String whose ending whitespace will will be removed.
|
mas01mj@732
|
121 *
|
mas01mj@732
|
122 * @returns A String with whitespace removed from the end
|
mas01mj@732
|
123 *
|
mas01mj@732
|
124 * @langversion ActionScript 3.0
|
mas01mj@732
|
125 * @playerversion Flash 9.0
|
mas01mj@732
|
126 * @tiptext
|
mas01mj@732
|
127 */
|
mas01mj@732
|
128 public static function rtrim(input:String):String
|
mas01mj@732
|
129 {
|
mas01mj@732
|
130 var size:Number = input.length;
|
mas01mj@732
|
131 for(var i:Number = size; i > 0; i--)
|
mas01mj@732
|
132 {
|
mas01mj@732
|
133 if(input.charCodeAt(i - 1) > 32)
|
mas01mj@732
|
134 {
|
mas01mj@732
|
135 return input.substring(0, i);
|
mas01mj@732
|
136 }
|
mas01mj@732
|
137 }
|
mas01mj@732
|
138
|
mas01mj@732
|
139 return "";
|
mas01mj@732
|
140 }
|
mas01mj@732
|
141
|
mas01mj@732
|
142 /**
|
mas01mj@732
|
143 * Determines whether the specified string begins with the spcified prefix.
|
mas01mj@732
|
144 *
|
mas01mj@732
|
145 * @param input The string that the prefix will be checked against.
|
mas01mj@732
|
146 *
|
mas01mj@732
|
147 * @param prefix The prefix that will be tested against the string.
|
mas01mj@732
|
148 *
|
mas01mj@732
|
149 * @returns True if the string starts with the prefix, false if it does not.
|
mas01mj@732
|
150 *
|
mas01mj@732
|
151 * @langversion ActionScript 3.0
|
mas01mj@732
|
152 * @playerversion Flash 9.0
|
mas01mj@732
|
153 * @tiptext
|
mas01mj@732
|
154 */
|
mas01mj@732
|
155 public static function beginsWith(input:String, prefix:String):Boolean
|
mas01mj@732
|
156 {
|
mas01mj@732
|
157 return (prefix == input.substring(0, prefix.length));
|
mas01mj@732
|
158 }
|
mas01mj@732
|
159
|
mas01mj@732
|
160 /**
|
mas01mj@732
|
161 * Determines whether the specified string ends with the spcified suffix.
|
mas01mj@732
|
162 *
|
mas01mj@732
|
163 * @param input The string that the suffic will be checked against.
|
mas01mj@732
|
164 *
|
mas01mj@732
|
165 * @param prefix The suffic that will be tested against the string.
|
mas01mj@732
|
166 *
|
mas01mj@732
|
167 * @returns True if the string ends with the suffix, false if it does not.
|
mas01mj@732
|
168 *
|
mas01mj@732
|
169 * @langversion ActionScript 3.0
|
mas01mj@732
|
170 * @playerversion Flash 9.0
|
mas01mj@732
|
171 * @tiptext
|
mas01mj@732
|
172 */
|
mas01mj@732
|
173 public static function endsWith(input:String, suffix:String):Boolean
|
mas01mj@732
|
174 {
|
mas01mj@732
|
175 return (suffix == input.substring(input.length - suffix.length));
|
mas01mj@732
|
176 }
|
mas01mj@732
|
177
|
mas01mj@732
|
178 /**
|
mas01mj@732
|
179 * Removes all instances of the remove string in the input string.
|
mas01mj@732
|
180 *
|
mas01mj@732
|
181 * @param input The string that will be checked for instances of remove
|
mas01mj@732
|
182 * string
|
mas01mj@732
|
183 *
|
mas01mj@732
|
184 * @param remove The string that will be removed from the input string.
|
mas01mj@732
|
185 *
|
mas01mj@732
|
186 * @returns A String with the remove string removed.
|
mas01mj@732
|
187 *
|
mas01mj@732
|
188 * @langversion ActionScript 3.0
|
mas01mj@732
|
189 * @playerversion Flash 9.0
|
mas01mj@732
|
190 * @tiptext
|
mas01mj@732
|
191 */
|
mas01mj@732
|
192 public static function remove(input:String, remove:String):String
|
mas01mj@732
|
193 {
|
mas01mj@732
|
194 return StringUtil.replace(input, remove, "");
|
mas01mj@732
|
195 }
|
mas01mj@732
|
196
|
mas01mj@732
|
197 /**
|
mas01mj@732
|
198 * Replaces all instances of the replace string in the input string
|
mas01mj@732
|
199 * with the replaceWith string.
|
mas01mj@732
|
200 *
|
mas01mj@732
|
201 * @param input The string that instances of replace string will be
|
mas01mj@732
|
202 * replaces with removeWith string.
|
mas01mj@732
|
203 *
|
mas01mj@732
|
204 * @param replace The string that will be replaced by instances of
|
mas01mj@732
|
205 * the replaceWith string.
|
mas01mj@732
|
206 *
|
mas01mj@732
|
207 * @param replaceWith The string that will replace instances of replace
|
mas01mj@732
|
208 * string.
|
mas01mj@732
|
209 *
|
mas01mj@732
|
210 * @returns A new String with the replace string replaced with the
|
mas01mj@732
|
211 * replaceWith string.
|
mas01mj@732
|
212 *
|
mas01mj@732
|
213 * @langversion ActionScript 3.0
|
mas01mj@732
|
214 * @playerversion Flash 9.0
|
mas01mj@732
|
215 * @tiptext
|
mas01mj@732
|
216 */
|
mas01mj@732
|
217 public static function replace(input:String, replace:String, replaceWith:String):String
|
mas01mj@732
|
218 {
|
mas01mj@732
|
219 return input.split(replace).join(replaceWith);
|
mas01mj@732
|
220 }
|
mas01mj@732
|
221
|
mas01mj@732
|
222
|
mas01mj@732
|
223 /**
|
mas01mj@732
|
224 * Specifies whether the specified string is either non-null, or contains
|
mas01mj@732
|
225 * characters (i.e. length is greater that 0)
|
mas01mj@732
|
226 *
|
mas01mj@732
|
227 * @param s The string which is being checked for a value
|
mas01mj@732
|
228 *
|
mas01mj@732
|
229 * @langversion ActionScript 3.0
|
mas01mj@732
|
230 * @playerversion Flash 9.0
|
mas01mj@732
|
231 * @tiptext
|
mas01mj@732
|
232 */
|
mas01mj@732
|
233 public static function stringHasValue(s:String):Boolean
|
mas01mj@732
|
234 {
|
mas01mj@732
|
235 //todo: this needs a unit test
|
mas01mj@732
|
236 return (s != null && s.length > 0);
|
mas01mj@732
|
237 }
|
mas01mj@732
|
238 }
|
mas01mj@732
|
239 } |