4 A collection of the String Object methods and functions.
12 Function: String.from {#String:String-from}
13 ------------------------------------
15 Returns the passed parameter as a String.
23 1. arg - (*mixed*) The argument to return as a string.
27 * (*string*) The argument as a string.
31 String.from(2); // returns '2'
32 String.from(true); // returns 'true'
36 Function: String.uniqueID {#String:String-uniqueID}
37 ---------------------------------------------------
47 * (*string*) A unique ID.
54 String method: test {#String:test}
55 ---------------------------
57 Searches for a match between the string and a regular expression.
58 For more information see [MDN Regexp:test][].
62 myString.test(regex[, params]);
66 1. regex - (*mixed*) The string or regular expression you want to match the string with.
67 2. params - (*string*, optional) If first parameter is a string, any parameters you want to pass to the regular expression ('g' has no effect).
71 * (*boolean*) `true`, if a match for the regular expression is found in this string.
72 * (*boolean*) `false` if is not found
76 'I like cookies'.test('cookie'); // returns true
77 'I like cookies'.test('COOKIE', 'i'); // returns true (ignore case)
78 'I like cookies'.test('cake'); // returns false
82 - [MDN Regular Expressions][]
86 String method: contains {#String:contains}
87 -----------------------------------
89 Checks to see if the string passed in is contained in this string.
90 If the position parameter is passed, it will only check for the string from that point.
94 myString.contains(string[, position]);
98 1. string - (*string*) The string to search for.
99 2. position - (*number*, optional) Position in the string to begin searching for `string`, defaults to `0`.
103 * (*boolean*) `true` if the string is contained in this string
104 * (*boolean*) `false` if not.
108 'a bc'.contains('bc'); // returns true
109 'abc'.contains('b', 1); // returns true
110 'abc'.contains('b', 2); // returns false
114 - [MDN String:indexOf][]
115 - [MDN String:contains][]
119 Since MooTools 1.5 the second parameter changed from `separator` to `position` so it conforms the ES6 specification.
120 If using the 1.4 compatibility layer, this method will be overwritten to have the old behavior.
122 String method: trim {#String:trim}
123 ---------------------------
125 Trims the leading and trailing spaces off a string.
133 * (*string*) The trimmed string.
137 ' i like cookies '.trim(); // returns 'i like cookies'
141 - [MDN String:trim][]
143 String method: clean {#String:clean}
144 -----------------------------
146 Removes all extraneous whitespace from a string and trims it ([String:trim][]).
154 * (*string*) The cleaned string.
158 ' i like cookies \n\n'.clean(); // returns 'i like cookies'
162 String method: camelCase {#String:camelCase}
163 -------------------------------------
165 Converts a hyphenated string to a camelcased string.
169 myString.camelCase();
173 * (*string*) The camelcased string.
177 'I-like-cookies'.camelCase(); // returns 'ILikeCookies'
181 String method: hyphenate {#String:hyphenate}
182 -------------------------------------
184 Converts a camelcased string to a hyphenated string.
188 myString.hyphenate();
192 * (*string*) The hyphenated string.
196 'ILikeCookies'.hyphenate(); // returns '-i-like-cookies'
200 String method: capitalize {#String:capitalize}
201 ---------------------------------------
203 Converts the first letter of each word in a string to uppercase.
207 myString.capitalize();
211 * (*string*) The capitalized string.
215 'i like cookies'.capitalize(); // returns 'I Like Cookies'
219 String method: escapeRegExp {#String:escapeRegExp}
220 -------------------------------------------
222 Escapes all regular expression characters from the string.
226 myString.escapeRegExp();
230 * (*string*) The escaped string.
234 'animals.sheep[1]'.escapeRegExp(); // returns 'animals\.sheep\[1\]'
238 String method: toInt {#String:toInt}
239 -----------------------------
241 Parses this string and returns a number of the specified radix or base.
245 myString.toInt([base]);
249 1. base - (*number*, optional) The base to use (defaults to 10).
253 * (*number*) The number.
254 * (*NaN*) If the string is not numeric, returns NaN.
258 '4em'.toInt(); // returns 4
259 '10px'.toInt(); // returns 10
267 String method: toFloat {#String:toFloat}
268 ---------------------------------
270 Parses this string and returns a floating point number.
278 * (*number*) The float.
279 * (*NaN*) If the string is not numeric, returns NaN.
283 '95.25%'.toFloat(); // returns 95.25
284 '10.848'.toFloat(); // returns 10.848
292 String method: hexToRgb {#String:hexToRgb}
293 -----------------------------------
295 Converts a hexadecimal color value to RGB. Input string must be in one of the following hexadecimal color formats (with or without the hash).
296 '#ffffff', #fff', 'ffffff', or 'fff'
300 myString.hexToRgb([array]);
304 1. array - (*boolean*, optional) If true is passed, will output an array (e.g. \[255, 51, 0\]) instead of a string (e.g. 'rgb(255, 51, 0)').
308 * (*string*) A string representing the color in RGB.
309 * (*array*) If the array flag is set, an array will be returned instead.
313 '#123'.hexToRgb(); // returns 'rgb(17, 34, 51)'
314 '112233'.hexToRgb(); // returns 'rgb(17, 34, 51)'
315 '#112233'.hexToRgb(true); // returns [17, 34, 51]
319 String method: rgbToHex {#String:rgbToHex}
320 -----------------------------------
322 Converts an RGB color value to hexadecimal. Input string must be in one of the following RGB color formats.
323 'rgb(255, 255, 255)', or 'rgba(255, 255, 255, 1)'
327 myString.rgbToHex([array]);
331 1. array - (*boolean*, optional) If true is passed, will output an array (e.g. \['ff', '33', '00'\]) instead of a string (e.g. '#ff3300').
335 * (*string*) A string representing the color in hexadecimal, or transparent if the fourth value of rgba in the input string is 0.
336 * (*array*) If the array flag is set, an array will be returned instead.
340 'rgb(17, 34, 51)'.rgbToHex(); // returns '#112233'
341 'rgb(17, 34, 51)'.rgbToHex(true); // returns ['11', '22', '33']
342 'rgba(17, 34, 51, 0)'.rgbToHex(); // returns 'transparent'
350 String method: substitute {#String:substitute}
351 ---------------------------------------
353 Substitutes keywords in a string using an object/array.
354 Removes undefined keywords and ignores escaped keywords.
358 myString.substitute(object[, regexp]);
362 1. object - (*mixed*) The key/value pairs used to substitute a string.
363 1. regexp - (*regexp*, optional) The regexp pattern to be used in the string keywords, with global flag. Defaults to /\\?\{([^}]+)\}/g .
367 * (*string*) - The substituted string.
371 var myString = '{subject} is {property_1} and {property_2}.';
372 var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'saviour'};
373 myString.substitute(myObject); // returns Jack Bauer is our lord and saviour
377 String method: stripScripts {#String:stripScripts}
378 ----------------------------------------------------
380 Strips the String of its *<script>* tags and anything in between them.
384 myString.stripScripts([evaluate]);
388 1. evaluate - (*boolean*, optional) If true is passed, the scripts within the String will be evaluated.
392 * (*string*) - The String without the stripped scripts.
396 var myString = "<script>alert('Hello')</script>Hello, World.";
397 myString.stripScripts(); // returns 'Hello, World.'
398 myString.stripScripts(true); // alerts 'Hello', then returns 'Hello, World.'
402 [MDN String]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String
403 [MDN String:contains]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/contains
404 [MDN String:indexOf]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf
405 [MDN String:trim]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/trim
406 [MDN Regexp:test]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp/test
407 [MDN Regular Expressions]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
408 [MDN parseInt]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt
409 [MDN parseFloat]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseFloat
410 [MDN Array]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array
411 [String:trim]: #String:trim
412 [Array:rgbToHex]: /core/Types/Array/#Array:rgbToHex
413 [String:trim]: #String:trim