A collection of the String Object methods and functions.
Returns the passed parameter as a String.
String.from(arg);
String.from(2); // returns '2' String.from(true); // returns 'true'
Generates a unique ID
String.uniqueID();
String.uniqueID();
Searches for a match between the string and a regular expression. For more information see MDC Regexp:test.
myString.test(regex[, params]);
true
, if a match for the regular expression is found in this string.false
if is not found'I like cookies'.test('cookie'); // returns true 'I like cookies'.test('COOKIE', 'i'); // returns true (ignore case) 'I like cookies'.test('cake'); // returns false
Checks to see if the string passed in is contained in this string. If the separator parameter is passed, will check to see if the string is contained in the list of values separated by that parameter.
myString.contains(string[, separator]);
true
if the string is contained in this stringfalse
if not.'a bc'.contains('bc'); // returns true 'a b c'.contains('c', ' '); // returns true 'a bc'.contains('b', ' '); // returns false
Trims the leading and trailing spaces off a string.
myString.trim();
' i like cookies '.trim(); // returns 'i like cookies'
Removes all extraneous whitespace from a string and trims it (String:trim).
myString.clean();
' i like cookies \n\n'.clean(); // returns 'i like cookies'
Converts a hyphenated string to a camelcased string.
myString.camelCase();
'I-like-cookies'.camelCase(); // returns 'ILikeCookies'
Converts a camelcased string to a hyphenated string.
myString.hyphenate();
'ILikeCookies'.hyphenate(); // returns '-i-like-cookies'
Converts the first letter of each word in a string to uppercase.
myString.capitalize();
'i like cookies'.capitalize(); // returns 'I Like Cookies'
Escapes all regular expression characters from the string.
myString.escapeRegExp();
'animals.sheep[1]'.escapeRegExp(); // returns 'animals\.sheep\[1\]'
Parses this string and returns a number of the specified radix or base.
myString.toInt([base]);
'4em'.toInt(); // returns 4 '10px'.toInt(); // returns 10
Parses this string and returns a floating point number.
myString.toFloat();
'95.25%'.toFloat(); // returns 95.25 '10.848'.toFloat(); // returns 10.848
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'
myString.hexToRgb([array]);
'#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)'
myString.rgbToHex([array]);
'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'
Substitutes keywords in a string using an object/array. Removes undefined keywords and ignores escaped keywords.
myString.substitute(object[, regexp]);
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 tags and anything in between them.
myString.stripScripts([evaluate]);
var myString = "<script>alert('Hello')</script>Hello, World."; myString.stripScripts(); // returns 'Hello, World.' myString.stripScripts(true); // alerts 'Hello', then returns 'Hello, World.'
© Linux.ria.ua, 2008-2024 |