Strings
Here is the documentation page for String.js.
String.test
Tests a string against a regular expression; optional second parameter for Regex options. Accepts regular expressions in both string and regexp mode.
"I like cookies".test("cookie"); // returns true
/*ignore case*/ "I like cookies".test("COOKIE", "i")
/*returns false*/ "I like cookies".test("cake");
/*here's an example of how you would probably actually use it*/ if("I like cookies".test("cake")) alert ('you like cake'); else alert("you don't like cake (what's wrong with you?)");
String.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. The big difference between this and String.test is that String.contains does not use regular expressions. The second argument also is notable, as it basically performs a String.split on the string and then searches the results:
'a b c'.contains('c', ' '); //true
'a bc'.contains('bc'); //true
'a bc'.contains('b', ' '); //false
String.escapeRegExp
Returns string with escaped regular expression characters:
'animals.sheeps[1]'.escapeRegExp(); //'animals\.sheeps\[1\]'
String.toInt
Parses the string to an integer.
"10".toInt(); // value is 10
"10px".toInt(); // value is 10
"-10".toInt(); // value is -10
"px10".toInt(); // NaN
"10.1".toInt(); // returns 10
String.toFloat
Parses a string to a float (a number with decimal values).
"10".toFloat(); // value is 10
"10.01".toFloat(); // value is 10.01
"-10.01".toFloat(); // value is -10.01
String.camelCase
Converts a hyphenated string into a mixed case string; mostly used by MooTools for setting and getting css properties.
"I-like-cookies".camelCase(); //"ILikeCookies"
String.hyphenate
Converts a CamelCase string to a hyphenated one; mostly used by MooTools for setting and getting css properties.
"ILikeCookies".hyphenate(); //"I-like-cookies"
String.capitalize
"i like cookies".capitalize(); //"I Like Cookies"
String.trim
" i like cookies ".trim() //"i like cookies"
String.clean
Trims (String.trim) a string AND removes all the double spaces in a string.
" i like cookies \n\n".clean() //"i like cookies"
String.rgbToHex
"rgb(17,34,51)".rgbToHex(); //"#112233"
"rgba(17,34,51,0)".rgbToHex(); //"transparent"
"rgb(17,34,51)".rgbToHex(true); //[11,22,33]
String.hexToRgb
"#112233".hexToRgb(); //"rgb(17,34,51)"
"#112233".hexToRgb(true); //[17,34,51]
String.stripScripts
This method removes all the <script> tags from a string. If you pass in true as an argument, those scripts will also be evaluated.
var myString = "<script>alert('Hello')</script>Hello, World."; console.log(myString.stripScripts()); /*Returns "Hello, World."*/ console.log(myString.stripScripts(true)); /*Alerts "Hello", then returns "Hello, World."*/
String.substitute
This method will subsitute the values in an object for the keys in a string. Each key must be wrapped with squigly brackets (you can change this if you want to use something else - see the docs).
var myString = "{subject} is {property_1} and {property_2}."; var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'savior'}; myString.substitute(myObject); //Jack Bauer is our lord and savior
mootorial/03-native/03-string.txt · Last modified: 2011/01/14 03:48 by ralph