contents   index   previous   next



String substr()

syntax:

string.substr(start, length)

where:

start - integer specifying the position within the string to begin the desired substring. If start is positive, the position is relative to the beginning of the string. If start is negative, the position is relative to the end of the string.

 

length - the length, in characters, of the substring to extract.

 

return:

string - a substring starting at position start and including the next number of characters specified by length.

 

description:

This method gets a section of a string. The start parameter is the first character in the new string. The length parameter determines how many characters to include in the new substring.

 

This method, substr() differs from String substring() in two basic ways. One, in substring() the start position cannot be negative, that is, it must be 0 or greater. Two, the second parameter in substring() indicates a position to go to, not the length of the new substring.

 

see:

String substring()

 

example:

var str = ("0123456789");

str.substr(0, 5)     // == "01234"

str.substr(2, 5)     // == "23456"

str.substr(-4, 2)    // == "56"

 


String substring()