String.js

Contains String prototypes.

License

MIT-style license.

Summary
String.jsContains String prototypes.
StringA collection of The String Object prototype methods.
Properties
testTests a string with a regular expression.
toIntparses a string to an integer.
toFloatparses a string to an float.
camelCaseConverts a hiphenated string to a camelcase string.
hyphenateConverts a camelCased string to a hyphen-ated string.
capitalizeConverts the first letter in each word of a string to Uppercase.
trimTrims the leading and trailing spaces off a string.
cleantrims (String.trim) a string AND removes all the double spaces in a string.
rgbToHexConverts an RGB value to hexidecimal.
hexToRgbConverts a hexidecimal color value to RGB.
containschecks if the passed in string is contained in the String.
escapeRegExpReturns string with escaped regular expression characters
rgbToHexsee String.rgbToHex, but as an array method.
hexToRgbsame as String.hexToRgb, but as an array method.

String

A collection of The String Object prototype methods.

Summary
Properties
testTests a string with a regular expression.
toIntparses a string to an integer.
toFloatparses a string to an float.
camelCaseConverts a hiphenated string to a camelcase string.
hyphenateConverts a camelCased string to a hyphen-ated string.
capitalizeConverts the first letter in each word of a string to Uppercase.
trimTrims the leading and trailing spaces off a string.
cleantrims (String.trim) a string AND removes all the double spaces in a string.
rgbToHexConverts an RGB value to hexidecimal.
hexToRgbConverts a hexidecimal color value to RGB.
containschecks if the passed in string is contained in the String.
escapeRegExpReturns string with escaped regular expression characters
rgbToHexsee String.rgbToHex, but as an array method.
hexToRgbsame as String.hexToRgb, but as an array method.

Properties

test

Tests a string with a regular expression.

Arguments

regexa string or regular expression object, the regular expression you want to match the string with
paramsoptional, if first parameter is a string, any parameters you want to pass to the regex (‘g’ has no effect)

Returns

true if a match for the regular expression is found in the string, false if not.  See http://developer.mozilla.org- /en- /docs- /Core_JavaScript_1.5_Reference:Objects:RegExp:test

Example

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

toInt

parses a string to an integer.

Returns

either an int or “NaN” if the string is not a number.

Example

var value = "10px".toInt(); // value is 10

toFloat

parses a string to an float.

Returns

either a float or “NaN” if the string is not a number.

Example

var value = "10.848".toFloat(); // value is 10.848

camelCase

Converts a hiphenated string to a camelcase string.

Example

"I-like-cookies".camelCase(); //"ILikeCookies"

Returns

the camel cased string

hyphenate

Converts a camelCased string to a hyphen-ated string.

Example

"ILikeCookies".hyphenate(); //"I-like-cookies"

capitalize

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

Example

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

Returns

the capitalized string

trim

Trims the leading and trailing spaces off a string.

Example

"    i like cookies     ".trim() //"i like cookies"

Returns

the trimmed string

clean

trims (String.trim) a string AND removes all the double spaces in a string.

Returns

the cleaned string

Example

" i      like     cookies      \n\n".clean() //"i like cookies"

rgbToHex

Converts an RGB value to hexidecimal.  The string must be in the format of “rgb(255,255,255)” or “rgba(255,255,255,1)”;

Arguments

arrayboolean value, defaults to false.  Use true if you want the array [‘FF’,’33’,’00’] as output instead of “#FF3300”

Returns

hex string or array. returns “transparent” if the output is set as string and the fourth value of rgba in input string is 0.

Example

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

hexToRgb

Converts a hexidecimal color value to RGB.  Input string must be the hex color value (with or without the hash).  Also accepts triplets (‘333’);

Arguments

arrayboolean value, defaults to false.  Use true if you want the array [255,255,255] as output instead of “rgb(255,255,255)”;

Returns

rgb string or array.

Example

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

contains

checks if the passed in string is contained in the String. also accepts an optional second parameter, to check if the string is contained in a list of separated values.

Example

'a b c'.contains('c', ' '); //true
'a bc'.contains('bc'); //true
'a bc'.contains('b', ' '); //false

escapeRegExp

Returns string with escaped regular expression characters

Example

var search = 'animals.sheeps[1]'.escapeRegExp(); // search is now 'animals\.sheeps\[1\]'

Returns

Escaped string

rgbToHex

see String.rgbToHex, but as an array method.

hexToRgb

same as String.hexToRgb, but as an array method.

Trims the leading and trailing spaces off a string.
Converts an RGB value to hexidecimal.
Converts a hexidecimal color value to RGB.

Documentation by Aaron Newton & Mootools Developers, generated by NaturalDocs and GeSHi