<HTML>
<Head>
<SCRIPT>
VaR STR = "123456 ";
Document. Write ("123456: <br/> ---------- substr ------------------- <br/> ");
Document. Write ("substr (0, 5) = |" + Str. substr (0, 5) + "|" + "<br/> ");
Document. Write ("substr (0) = |" + Str. substr (0) + "|" + "<br/> ");
Document. Write ("substr (5, 1) = |" + Str. substr (5, 1) + "|" + "<br/> ");
Document. Write ("substr (-5, 2) = |" + Str. substr (-5, 2) + "|" + "<br/> ");
Document. Write ("substr (-2,-5) = |" + Str. substr (-2,-5) + "|" + "<br/> ");
Document. Write ("substr (1,1) = |" + Str. substr (1,1) + "|" + "<br/> ");
Document. Write ("----------------- substring --------------- <br/> ");
document. write ("substring (0, 5) = |" + Str. substring (0, 5) + "|" + "
");
document. write ("substring (0) = |" + Str. substring (0) + "|" + "
");
document. write ("substring (5, 1) = |" + Str. substring (5, 1) + "|" + "
");
document. write ("substring (-5, 2) = |" + Str. substring (-5, 2) + "|" + "
");
document. write ("substring (-2,-5) = |" + Str. substring (-2,-5) + "|" + "
");
document. write ("substring (2,-5) = |" + Str. substring (2,-5) + "|" + "
");
document. write ("substring (1, 1) = |" + Str. substring (1, 1) + "|" + "
");
document. write ("----------------- slice ---------------
");
document. write ("slice (0, 5) = |" + Str. slice (0, 5) + "|" + "
");
document. write ("slice (0) = |" + Str. slice (0) + "|" + "
");
document. write ("slice (5, 1) = |" + Str. slice (5, 1) + "|" + "
");
document. write ("slice (-5, 2) = |" + Str. slice (-5, 2) + "|" + "
");
document. write ("slice (-2,-5) = |" + Str. slice (-2,-5) + "|" + "
");
document. write ("slice (2,-5) = |" + Str. SLI Ce (2,-5) + "|" + "
");
document. write ("slice (1, 1) = |" + Str. slice (1, 1) + "|" + "
");
/**
conclusion:
substring (START, end)
the substring returned by the substring method contains the start character, but not the end character.
If start and end are equal, this method returns an empty string (a string with a length of 0 ).
If start is greater than end, this method swaps the two parameters before extracting the substring. The position is automatically reversed. The value is greater than the start position, and the value is smaller than the end position.
If start or end is negative, it is replaced with 0.
Stringobject. substr (start [, length])
Start is required. The starting position of the required substring. The index of the first character in the string is 0.
Length is optional. Number of characters to be included in the returned substring
If the length is 0 or negative, an empty string is returned. Convert the value to 0. If start or end is Nan or negative, replace it with 0.
If this parameter is not specified, the substring will continue to the end of the stringobject
Stringobject. Slice (startindex, endindex)
1) The endindex parameter is optional. If it is not specified, the default value is the string length stringobject. length.
2) startindex and endindex can be negative. If the value is negative, the value starts from the end of the string. -1 indicates the last character of the string.
VaR stringobject = "Hello world! ";
Alert (stringobject. Slice (-3); // lD!
Alert (stringobject. Slice (-3, stringobject. Length); // lD!
Alert (stringobject. Slice (-3,-1); // LD
3) Both startindex and endindex are optional. If none are specified, all stringobject strings are returned, which is equivalent to slice (0)
VaR stringobject = "Hello world! ";
Alert (stringobject. Slice (); // Hello world!
Alert (stringobject. Slice (0); // Hello world!
4) if startindex and endindex are equal, an empty string is returned.
**/
</SCRIPT>
</Head>
<Body>
</Body>
</Html>