function SubstrDemo(){
var s, ss; // 声明变量。
var s = "The rain in Spain falls mainly in the plain.";
ss = s.substr(12, 5); // 获取子字符串。
return(ss); // 返回 "Spain"。
}
substr Method
Returns a substring beginning at a specified location and having a specified length.
stringvar.substr(start [, length ])
Arguments
stringvar
Required. A string literal or String object from which the substring is extracted.
start
Required. The starting position of the desired substring. The index of the first character in the string is zero.
length
Optional. The number of characters to include in the returned substring.
Remarks
If length is zero or negative, an empty string is returned. If not specified, the substring continues to the end of stringvar.
Example
The following example illustrates the use of the substr method.
function SubstrDemo(){
var s, ss; //Declare variables.
var s = "The rain in Spain falls mainly in the plain.";
ss = s.substr(12, 5); //Get substring.
return(ss); // Returns "Spain".
}
Description
Returns a substring beginning at a specified location and having a specified length.
Syntax
stringvar.substr(start [, length ])
The substr method syntax has these parts:
Part Description
stringvar Required. A string literal or String object from which the substring is extracted.
start Required. The starting position of the desired substring. The index of the first character in the string is zero.
length Optional. The number of characters to include in the returned substring.
Remarks
If length is zero or negative, an empty string is returned. If not specified, the substring continues to the end of stringvar.
The following example illustrates the use of the substr method:
function SubstrDemo()
{
var s, ss;
var s = "The quick brown fox jumped over the lazy yellow dog.";
ss = s.substr(16, 3);
// Returns "fox"
return(ss);
}