length
|
The number of characters in the string.
> new String('four').length;
4
|
charAt(position)
|
Returns the character at the specified position. Positions start at 0.
> "script".charAt(0);
"s"
Since ES5, it's also possible to use array notation for the same purpose. (This feature has been long supported in many browsers before ES5, but not IE)
> "script"[0];
"s"
|
charCodeAt(position)
|
Returns the numeric code (Unicode) of the character at the specified position.
> "script".charCodeAt(0);
115
|
concat(str1, str2, ....)
|
Return a new string glued from the input pieces.
> "".concat('zig', '-', 'zag');
"zig-zag"
|
indexOf(needle, start)
|
If the needle matches a part of the string, the position of the match is returned. The optional second parameter defines where the search should start from. Returns -1 if no match is found.
> "javascript".indexOf('scr');
4
> "javascript".indexOf('scr', 5);
-1
|
lastIndexOf(needle, start)
|
Same as indexOf() but starts the search from the end of the string. The last occurrence of a :
> "javascript".lastIndexOf('a');
3
|
localeCompare(needle)
|
Compares two strings in the current locale. Returns 0 if the two strings are equal, 1 if the needle gets sorted before the string object, -1 otherwise.
> "script".localeCompare('crypt');
1
> "script".localeCompare('sscript');
-1
> "script".localeCompare('script');
0
|
match(regexp)
|
Accepts a regular expression object and returns an array of matches.
> "R2-D2 and C-3PO".match(/[0-9]/g);
["2", "2", "3"]
|
replace(needle, replacement)
|
Allows you to replace the matching results of a regexp pattern. The replacement can also be a callback function. Capturing groups are available as $1, $2,...$9 .
> "R2-D2".replace(/2/g, '-two');
"R-two-D-two"
> "R2-D2".replace(/(2)/g, '$1$1');
"R22-D22"
|
search(regexp)
|
Returns the position of the first regular expression match.
> "C-3PO".search(/[0-9]/);
2
|
slice(start, end)
|
Returns the part of a string identified by the start and end positions. If start is negative, the start position is length + start , similarly if the end parameter is negative, the end position is length + end.
> "R2-D2 and C-3PO".slice(4, 13);
"2 and C-3"
> "R2-D2 and C-3PO".slice(4, -1);
"2 and C-3P"
|
split(separator, limit)
|
Turns a string into an array. The second parameter, limit, is optional. As with replace() , search() , and match() , the separator is a regular expression but can also be a string.
> "1,2,3,4".split(/,/);
["1", "2", "3", "4"]
> "1,2,3,4".split(',', 2);
["1", "2"]
|
substring(start, end)
|
Similar to slice() . When start or end are negative or invalid, they are considered 0. If they are greater than the string length, they are considered to be the length. If end is greater than start , their values are swapped.
> "R2-D2 and C-3PO".substring(4, 13);
"2 and C-3"
> "R2-D2 and C-3PO".substring(13, 4);
"2 and C-3"
|
toLowerCase()
toLocaleLowerCase()
|
Transforms the string to lowercase.
> "Java".toLowerCase();
"java"
|
toUpperCase()
toLocaleUpperCase()
|
Transforms the string to uppercase.
> "Script".toUpperCase();
"SCRIPT"
|