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