String. substr (N1, N2) is a string that is commonly used to extract the specified length (N2) from the specified position (N1;
String. substring (N1, N2), which is commonly used from the specified position (N1)
String at the specified position (N2;
There are also the following statements:
String. substr (N1, N2) is a string that is commonly used to extract the specified length (N2) from the specified position (N1;
String. substring (N1, N2) is a little special. It first finds a small value from N1, N2, and then starts from the starting position of the string, intercept a string between a smaller value position and a smaller value. The length of the truncated string is the difference between a smaller value and a smaller value.
// Replace substring with substr. The result is the same.
<HTML>
<Head>
<Title> exercise/Lab </title>
<SCRIPT type = "text/JavaScript">
Function lab16 (){
VaR entry = prompt ("enter a string-at least 5 Alpha characters in lower case ","");
VaR entrylength = entry. length;
Document. Write ("<H2> you entered" + entry + "</H2> ");
If (entrylength % 2 = 0) {/* Check entry length for remainder */
Document. Write ("<H2> even number of characters </H2> <br/> ");
Document. Write ("<H2>" + entry. touppercase () + "</H2> ");
}
Else {
Document. Write ("<H2> odd number of characters </H2> <br/> ");
VaR midpoint = math. Floor (entrylength/2 );
Document. Write ("<H2>" + entry. substring (0, midpoint). tolowercase () +
"<Font color = '# ff0000'>" + entry. substr (midpoint, 1). touppercase () + "</font>" +
Entry. substring (midpoint + 1). tolowercase () + "</H2> ");
}
}
</SCRIPT>
</Head>
<Body>
<H2> lab 16 </H2>
<Script language = 'javascript 'Type = 'text/JavaScript '>
<! --
Lab16 ();
-->
</SCRIPT>
</Body>
</Html>