JavaScript Reference
Examples:
var magic = "Abracadabra";
magic.lastIndexOf("abra", 8) //Returns 7
magic.lastIndexOf("abra", 6) //Returns -1
magic.toLowerCase().lastIndexOf("abra", 6) //Returns 0
magic.lastIndexOf("abra") //Returns 7
slice()
Syntax:
string .slice(begin )
string .slice(begin , end )
This method returns a portion (substring ) of string containing all characte rs from
and including the character at the p osition begin and up to but not including the
character at the position end . The meth od does not change string . The arguments
begin and end can also assume negative values, in which case they specify positions
counted from the end of the string . For example, -1 indicates the last cha racter, -2
the second to last character, and so forth. I f the end argument is omitted, then all
characters from begin to the end of the string are returned.
It is impo rtant to remember that the character at the position begin is included in
the returned substring while the ch aracter at the position end is not. This may seem
strange, but there are some noteworthy implications of this rule. Fir st, the len gth of
the returned string is always end - begin . Second, if you want to make several
contiguous slices of string , which you don’t want to overlap, you simply use the
end argum ent of the preced ing slice as the begin argum ent of the current slice.
Examples:
var s = "ABCDE";
s.slice(1) //Returns "BCDE"
s.slice(-1) //Returns "E" (the last character)
s.slice(0, 1) //Returns "A"
s.slice(1, -2) //Returns "BC"
split()
Syntax:
string .split()
string .split(delimiter )
This method creates and retur ns an array o f strings obtained by splittin g string into
substrings. The split() method first searches for all the occurrences of delimiter
E.16. String (Core JavaScript) 425