1 Native: String {#String}
2 ========================
4 A collection of the String Object prototype methods.
12 String Method: test {#String:test}
13 ----------------------------------
15 Searches for a match between the string and a regular expression.
16 For more information see [MDC Regexp:test][].
20 myString.test(regex[,params]);
24 1. regex - (*mixed*) The string or regular expression you want to match the string with.
25 2. params - (*string*, optional) If first parameter is a string, any parameters you want to pass to the regular expression ('g' has no effect).
29 * (*boolean*) `true`, if a match for the regular expression is found in this string.
30 * (*boolean*) `false` if is not found
34 "I like cookies".test("cookie"); //returns true
35 "I like cookies".test("COOKIE", "i"); //returns true (ignore case)
36 "I like cookies".test("cake"); //returns false
40 - [MDC Regular Expressions][]
44 String Method: contains {#String:contains}
45 ------------------------------------------
47 Checks to see if the string passed in is contained in this string.
48 If the separator parameter is passed, will check to see if the string is contained in the list of values separated by that parameter.
52 myString.contains(string[, separator]);
56 1. string - (*string*) The string to search for.
57 2. separator - (*string*, optional) The string that separates the values in this string (eg. Element classNames are separated by a ' ').
61 * (*boolean*) `true` if the string is contained in this string
62 * (*boolean*) `false` if not.
66 'a bc'.contains('bc'); //returns true
67 'a b c'.contains('c', ' '); //returns true
68 'a bc'.contains('b', ' '); //returns false
72 String Method: trim {#String:trim}
73 ----------------------------------
75 Trims the leading and trailing spaces off a string.
83 * (*string*) The trimmed string.
87 " i like cookies ".trim(); //"i like cookies"
91 String Method: clean {#String:clean}
92 ------------------------------------
94 Removes all extraneous whitespace from a string and trims it ([String:trim][]).
102 * (*string*) The cleaned string.
106 " i like cookies \n\n".clean(); //returns "i like cookies"
110 String Method: camelCase {#String:camelCase}
111 --------------------------------------------
113 Converts a hyphenated string to a camelcased string.
117 myString.camelCase();
121 * (*string*) The camelcased string.
125 "I-like-cookies".camelCase(); //returns "ILikeCookies"
129 String Method: hyphenate {#String:hyphenate}
130 --------------------------------------------
132 Converts a camelcased string to a hyphenated string.
136 myString.hyphenate();
140 * (*string*) The hyphenated string.
144 "ILikeCookies".hyphenate(); //returns "I-like-cookies"
148 String Method: capitalize {#String:capitalize}
149 ----------------------------------------------
151 Converts the first letter of each word in a string to uppercase.
155 myString.capitalize();
159 * (*string*) The capitalized string.
163 "i like cookies".capitalize(); //returns "I Like Cookies"
167 String Method: escapeRegExp {#String:escapeRegExp}
168 --------------------------------------------------
170 Escapes all regular expression characters from the string.
174 myString.escapeRegExp();
178 * (*string*) The escaped string.
182 'animals.sheep[1]'.escapeRegExp(); //returns 'animals\.sheep\[1\]'
186 String Method: toInt {#String:toInt}
187 ------------------------------------
189 Parses this string and returns a number of the specified radix or base.
193 myString.toInt([base]);
197 1. base - (*number*, optional) The base to use (defaults to 10).
201 * (*number*) The number.
202 * (*NaN*) If the string is not numeric, returns NaN.
206 "4em".toInt(); //returns 4
207 "10px".toInt(); //returns 10
215 String Method: toFloat {#String:toFloat}
216 ----------------------------------------
218 Parses this string and returns a floating point number.
226 * (*number*) The float.
227 * (*NaN*) If the string is not numeric, returns NaN.
231 "95.25%".toFloat(); //returns 95.25
232 "10.848".toFloat(); //returns 10.848
240 String Method: hexToRgb {#String:hexToRgb}
241 ------------------------------------------
243 Converts a hexidecimal color value to RGB. Input string must be in one of the following hexidecimal color formats (with or without the hash).
244 '#ffffff', #fff', 'ffffff', or 'fff'
248 myString.hexToRgb([array]);
252 1. array - (*boolean*, optional) If true is passed, will output an array (eg. \[255, 51, 0\]) instead of a string (eg. "rgb(255,51,0)").
256 * (*string*) A string representing the color in RGB.
257 * (*array*) If the array flag is set, an array will be returned instead.
261 "#123".hexToRgb(); //returns "rgb(17,34,51)"
262 "112233".hexToRgb(); //returns "rgb(17,34,51)"
263 "#112233".hexToRgb(true); //returns [17, 34, 51]
267 String Method: rgbToHex {#String:rgbToHex}
268 ------------------------------------------
270 Converts an RGB color value to hexidecimal. Input string must be in one of the following RGB color formats.
271 "rgb(255,255,255)", or "rgba(255,255,255,1)"
275 myString.rgbToHex([array]);
279 1. array - (*boolean*, optional) If true is passed, will output an array (eg. \['ff','33','00'\]) instead of a string (eg. "#ff3300").
283 * (*string*) A string representing the color in hexadecimal, or transparent if the fourth value of rgba in the input string is 0.
284 * (*array*) If the array flag is set, an array will be returned instead.
288 "rgb(17,34,51)".rgbToHex(); //returns "#112233"
289 "rgb(17,34,51)".rgbToHex(true); //returns ['11','22','33']
290 "rgba(17,34,51,0)".rgbToHex(); //returns "transparent"
298 String Method: stripScripts {#String:stripScripts}
299 ------------------------------------------
301 Strips the String of its `<script>` tags and anything in between them.
305 myString.stripScripts([evaluate]);
309 1. evaluate - (*boolean*, optional) If true is passed, the scripts within the String will be evaluated.
313 * (*string*) - The String without the stripped scripts.
317 var myString = "<script>alert('Hello')</script>Hello, World.";
318 myString.stripScripts(); //Returns "Hello, World."
319 myString.stripScripts(true); //Alerts "Hello", then returns "Hello, World."
322 String Method: substitute {#String:substitute}
323 ------------------------------------------
325 Substitutes keywords in a string using an object/array.
326 Removes undefined keywords and ignores escaped keywords.
330 myString.substitute(object[, regexp]);
334 1. object - (*mixed*) The key/value pairs used to substitute a string.
335 1. regexp - (*regexp*, optional) The regexp pattern to be used in the string keywords, with global flag. Defaults to /\\?\{([^}]+)\}/g .
339 * (*string*) - The substituted string.
343 var myString = "{subject} is {property_1} and {property_2}.";
344 var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'savior'};
345 myString.substitute(myObject); //Jack Bauer is our lord and savior
349 [MDC String]: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String
350 [MDC Regexp:test]: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:RegExp:test
351 [MDC Regular Expressions]: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Regular_Expressions
352 [MDC parseInt]: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:parseInt
353 [MDC parseFloat]: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:parseFloat
354 [MDC Array]: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array
355 [String:trim]: #String:trim
356 [Array:rgbToHex]: /core/Native/Array/#Array:rgbToHex