A collection of the String Object methods and functions.

See Also:

Returns the passed parameter as a String.

Syntax:

String.convert(arg);

Arguments:

  1. arg - (mixed) The argument to return as a string.

Returns:

  • (string) The argument as a string.

Example:

String.convert(2); // returns '2'
String.convert(true); // returns 'true'

Generates a unique ID

Syntax:

String.uniqueID();

Returns:

  • (string) A unique ID.

Example:

String.uniqueID();

Searches for a match between the string and a regular expression. For more information see MDN Regexp:test.

Syntax:

myString.test(regex[, params]);

Arguments:

  1. regex - (mixed) The string or regular expression you want to match the string with.
  2. params - (string, optional) If first parameter is a string, any parameters you want to pass to the regular expression ('g' has no effect).

Returns:

  • (boolean) true, if a match for the regular expression is found in this string.
  • (boolean) false if is not found

Examples:

'I like cookies'.test('cookie'); // returns true
'I like cookies'.test('COOKIE', 'i'); // returns true (ignore case)
'I like cookies'.test('cake'); // returns false

See Also:

Checks to see if the string passed in is contained in this string. If the position parameter is passed, it will only check for the string from that point.

Syntax:

myString.contains(string[, position]);

Arguments:

  1. string - (string) The string to search for.
  2. position - (number, optional) Position in the string to begin searching for string, defaults to 0.

Returns:

  • (boolean) true if the string is contained in this string
  • (boolean) false if not.

Examples:

'a bc'.contains('bc'); // returns true
'abc'.contains('b', 1); // returns true
'abc'.contains('b', 2); // returns false

See Also:

Note:

Since MooTools 1.5 the second parameter changed from separator to position so it conforms the ES6 specification. If using the 1.4 compatibility layer, this method will be overwritten to have the old behavior.

Trims the leading and trailing spaces off a string.

Syntax:

myString.trim();

Returns:

  • (string) The trimmed string.

Examples:

'    i like cookies     '.trim(); // returns 'i like cookies'

See Also:

Removes all extraneous whitespace from a string and trims it (String:trim).

Syntax:

myString.clean();

Returns:

  • (string) The cleaned string.

Examples:

' i      like     cookies      \n\n'.clean(); // returns 'i like cookies'

Converts a hyphenated string to a camelcased string.

Syntax:

myString.camelCase();

Returns:

  • (string) The camelcased string.

Examples:

'I-like-cookies'.camelCase(); // returns 'ILikeCookies'

Converts a camelcased string to a hyphenated string.

Syntax:

myString.hyphenate();

Returns:

  • (string) The hyphenated string.

Examples:

'ILikeCookies'.hyphenate(); // returns '-i-like-cookies'

Converts the first letter of each word in a string to uppercase.

Syntax:

myString.capitalize();

Returns:

  • (string) The capitalized string.

Examples:

'i like cookies'.capitalize(); // returns 'I Like Cookies'

Escapes all regular expression characters from the string.

Syntax:

myString.escapeRegExp();

Returns:

  • (string) The escaped string.

Examples:

'animals.sheep[1]'.escapeRegExp(); // returns 'animals\.sheep\[1\]'

Parses this string and returns a number of the specified radix or base.

Syntax:

myString.toInt([base]);

Arguments:

  1. base - (number, optional) The base to use (defaults to 10).

Returns:

  • (number) The number.
  • (NaN) If the string is not numeric, returns NaN.

Examples:

'4em'.toInt(); // returns 4
'10px'.toInt(); // returns 10

See Also:

Parses this string and returns a floating point number.

Syntax:

myString.toFloat();

Returns:

  • (number) The float.
  • (NaN) If the string is not numeric, returns NaN.

Examples:

'95.25%'.toFloat(); // returns 95.25
    '10.848'.toFloat(); // returns 10.848

See Also:

Converts a hexadecimal color value to RGB. Input string must be in one of the following hexadecimal color formats (with or without the hash). '#ffffff', #fff', 'ffffff', or 'fff'

Syntax:

myString.hexToRgb([array]);

Arguments:

  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)').

Returns:

  • (string) A string representing the color in RGB.
  • (array) If the array flag is set, an array will be returned instead.

Examples:

'#123'.hexToRgb(); // returns 'rgb(17, 34, 51)'
'112233'.hexToRgb(); // returns 'rgb(17, 34, 51)'
'#112233'.hexToRgb(true); // returns [17, 34, 51]

Converts an RGB color value to hexadecimal. Input string must be in one of the following RGB color formats. 'rgb(255, 255, 255)', or 'rgba(255, 255, 255, 1)'

Syntax:

myString.rgbToHex([array]);

Arguments:

  1. array - (boolean, optional) If true is passed, will output an array (e.g. ['ff', '33', '00']) instead of a string (e.g. '#ff3300').

Returns:

  • (string) A string representing the color in hexadecimal, or transparent if the fourth value of rgba in the input string is 0.
  • (array) If the array flag is set, an array will be returned instead.

Examples:

'rgb(17, 34, 51)'.rgbToHex(); // returns '#112233'
'rgb(17, 34, 51)'.rgbToHex(true); // returns ['11', '22', '33']
'rgba(17, 34, 51, 0)'.rgbToHex(); // returns 'transparent'

See Also:

Substitutes keywords in a string using an object/array. Removes undefined keywords and ignores escaped keywords.

Syntax:

myString.substitute(object[, regexp]);

Arguments:

  1. object - (mixed) The key/value pairs used to substitute a string.
  2. regexp - (regexp, optional) The regexp pattern to be used in the string keywords, with global flag. Defaults to /\?{([^}]+)}/g .

Returns:

  • (string) - The substituted string.

Examples:

var myString = '{subject} is {property_1} and {property_2}.';
var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'saviour'};
myString.substitute(myObject); // returns Jack Bauer is our lord and saviour

Strips the String of its <script> tags and anything in between them.

Syntax:

myString.stripScripts([evaluate]);

Arguments:

  1. evaluate - (boolean, optional) If true is passed, the scripts within the String will be evaluated.

Returns:

  • (string) - The String without the stripped scripts.

Examples:

var myString = "<script>alert('Hello')</script>Hello, World.";
myString.stripScripts(); // returns 'Hello, World.'
myString.stripScripts(true); // alerts 'Hello', then returns 'Hello, World.'