1 <HTML> 2 3 <HEAD> 4 <TITLE>JavaScript Example: Strings</TITLE> 5 </HEAD> 6 7 <BODY BGCOLOR="#FFFFFF"> 8 9 <SCRIPT LANGUAGE="JavaScript"> 10 <!-- hide script from old browsers 11 12 var head = "Introductory Short Course on Web Technologies"; 13 document.writeln("<H2>" + head + "</H2>"); 14 document.writeln("With respect to the above heading:"); 15 16 document.writeln("<UL>"); 17 18 var len = head.length; 19 document.write("<LI>the number of characters is: "); 20 document.writeln(len + "<P>"); 21 document.write("<LI>the first character is: \""); 22 document.writeln(head.charAt(0) + "\"<P>"); 23 document.write("<LI>the last character is: \""); 24 document.writeln(head.charAt(len-1) + "\"<P>"); 25 26 document.write("<LI>the position of the first letter \"S\" is: "); 27 var pos1 = head.indexOf("S"); 28 document.writeln(pos1 + "<P>"); 29 document.write("<LI>the position of the first letter \"e\" is: "); 30 var pos2 = head.indexOf("e"); 31 document.writeln(pos2 + "<P>"); 32 document.write("<LI>a substring beginning with \"S\" "); 33 document.write("and ending with \"e\" is: \""); 34 document.writeln(head.substring(pos1, pos2+1) + "\"<P>"); 35 36 // Count the number of "o"s: 37 var m = -1, n = 0; 38 var lastPos = head.lastIndexOf("o"); 39 while ( m < lastPos ) { 40 n++; 41 m = head.indexOf("o", m+1); 42 } 43 document.write("<LI>the number of occurrences of "); 44 document.write("the letter \"o\" is: "); 45 document.writeln(n); 46 47 document.writeln("</UL>"); 48 49 // end script hiding --> 50 </SCRIPT> 51 52 </BODY> 53 54 </HTML>