1 <!DOCTYPE html> 2 <html> 3 <body> 4 <title>統計字符串中某個字符的個數</title> 5 <script> 6 var countryList=["one","two","three","four","five","six"]; 7 var count=0; 8 document.write("在如下字符中:<br/>"); 9 for(var i in countryList){ 10 document.write(countryList[i]+" "); 11 if(countryList[i].indexOf("o")!=-1) 12 count++; 13 } 14 document.write("<br/> 字符o有"+count+"個。"); 15 </script> 16 </body> 17 </html>