Rename <Type>.from to <Type>.convert
[mootools.git] / Docs / Types / String.md
blobd58dd0af9727a49dacac9e58ad0d1df8a889d7d5
1 Type: String {#String}
2 ====================
4 A collection of the String Object methods and functions.
6 ### See Also:
8 - [MDN String][]
12 Function: String.convert {#String:String:convert}
13 ------------------------------------
15 Returns the passed parameter as a String.
17 ### Syntax:
19         String.convert(arg);
21 ### Arguments:
23 1. arg - (*mixed*) The argument to return as a string.
25 ### Returns:
27 * (*string*) The argument as a string.
29 ### Example:
31         String.convert(2); // returns '2'
32         String.convert(true); // returns 'true'
36 Function: String.uniqueID {#String:String-uniqueID}
37 ---------------------------------------------------
39 Generates a unique ID
41 ### Syntax:
43         String.uniqueID();
45 ### Returns:
47 * (*string*) A unique ID.
49 ### Example:
51         String.uniqueID();
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][].
60 ### Syntax:
62         myString.test(regex[, params]);
64 ### Arguments:
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).
69 ### Returns:
71 * (*boolean*) `true`, if a match for the regular expression is found in this string.
72 * (*boolean*) `false` if is not found
74 ### Examples:
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
80 ### See Also:
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.
92 ### Syntax:
94         myString.contains(string[, position]);
96 ### Arguments:
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`.
101 ### Returns:
103 * (*boolean*) `true` if the string is contained in this string
104 * (*boolean*) `false` if not.
106 ### Examples:
108         'a bc'.contains('bc'); // returns true
109         'abc'.contains('b', 1); // returns true
110         'abc'.contains('b', 2); // returns false
112 ### See Also:
114 - [MDN String:indexOf][]
115 - [MDN String:contains][]
117 ### Note:
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.
127 ### Syntax:
129         myString.trim();
131 ### Returns:
133 * (*string*) The trimmed string.
135 ### Examples:
137         '    i like cookies     '.trim(); // returns 'i like cookies'
139 ### See Also:
141 - [MDN String:trim][]
143 String method: clean {#String:clean}
144 -----------------------------
146 Removes all extraneous whitespace from a string and trims it ([String:trim][]).
148 ### Syntax:
150         myString.clean();
152 ### Returns:
154 * (*string*) The cleaned string.
156 ### Examples:
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.
167 ### Syntax:
169         myString.camelCase();
171 ### Returns:
173 * (*string*) The camelcased string.
175 ### Examples:
177         'I-like-cookies'.camelCase(); // returns 'ILikeCookies'
181 String method: hyphenate {#String:hyphenate}
182 -------------------------------------
184 Converts a camelcased string to a hyphenated string.
186 ### Syntax:
188         myString.hyphenate();
190 ### Returns:
192 * (*string*) The hyphenated string.
194 ### Examples:
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.
205 ### Syntax:
207         myString.capitalize();
209 ### Returns:
211 * (*string*) The capitalized string.
213 ### Examples:
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.
224 ### Syntax:
226         myString.escapeRegExp();
228 ### Returns:
230 * (*string*) The escaped string.
232 ### Examples:
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.
243 ### Syntax:
245         myString.toInt([base]);
247 ### Arguments:
249 1. base - (*number*, optional) The base to use (defaults to 10).
251 ### Returns:
253 * (*number*) The number.
254 * (*NaN*) If the string is not numeric, returns NaN.
256 ### Examples:
258         '4em'.toInt(); // returns 4
259         '10px'.toInt(); // returns 10
261 ### See Also:
263 - [MDN parseInt][]
267 String method: toFloat {#String:toFloat}
268 ---------------------------------
270 Parses this string and returns a floating point number.
272 ### Syntax:
274         myString.toFloat();
276 ### Returns:
278 * (*number*) The float.
279 * (*NaN*) If the string is not numeric, returns NaN.
281 ### Examples:
283                 '95.25%'.toFloat(); // returns 95.25
284                 '10.848'.toFloat(); // returns 10.848
286 ### See Also:
288 - [MDN parseFloat][]
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'
298 ### Syntax:
300         myString.hexToRgb([array]);
302 ### Arguments:
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)').
306 ### Returns:
308 * (*string*) A string representing the color in RGB.
309 * (*array*) If the array flag is set, an array will be returned instead.
311 ### Examples:
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)'
325 ### Syntax:
327         myString.rgbToHex([array]);
329 ### Arguments:
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').
333 ### Returns:
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.
338 ### Examples:
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'
344 ### See Also:
346 - [Array:rgbToHex][]
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.
356 ### Syntax:
358         myString.substitute(object[, regexp]);
360 ### Arguments:
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 .
365 ### Returns:
367 * (*string*) - The substituted string.
369 ### Examples:
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.
382 ### Syntax:
384         myString.stripScripts([evaluate]);
386 ### Arguments:
388 1. evaluate - (*boolean*, optional) If true is passed, the scripts within the String will be evaluated.
390 ### Returns:
392 * (*string*) - The String without the stripped scripts.
394 ### Examples:
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