Page 226 - Web_Application_v2.0_C12_Fb
P. 226
1. The slice() Method: The slice() method extracts a section of a string and returns it as a new string, without
modifying the original string.
Example 31: To demonstrate the use of slice method
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>slice() Method</TITLE>
</HEAD>
<BODY>
<H2>The slice() Method</H2>
<P>The slice() method extracts a section of a string.</P>
<P id="demo1"></P>
<SCRIPT>
var text = "Hello, JavaScript!";
document.getElementById("demo1").innerHTML = text.slice(7, 17);
</SCRIPT>
</BODY>
</HTML>
Output:
2. The substring() Method: The substring() method extracts characters between two specified indices and
returns a new string.
Example 32: To demonstrate the use of substring method
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>substring() Method</TITLE>
</HEAD>
<BODY>
<H2>The substring() Method</H2>
<P>The substring() method extracts part of a string.</P>
<P id="demo1"></P>
<SCRIPT>
var text = "Hello, JavaScript!";
224 Touchpad Web Applications (Ver. 2.0)-XII

