2
0

StringUtil.as 6.6 KB

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