用JavaScript统计包含“a”或“A”的字符串个数,怎么做??
下面详解。
<pre name="code" class="html"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>统计包含“a”或“A”的字符串的个数</title>
<script type="text/javascript">
var countryList=["America","Greece","Britain","Canada","China","Egypt"];
var count=0;
document.write("在以下字符中:<br/>");
for(var i in countryList){
document.write(countryList[i]+"<br/>");
if(countryList[i].indexOf("a")!=-1||countryList[i].indexOf("A")!=-1)
count++;
}
document.write("共有"+count+"个字符中串包含a或A。");
</script>
</head>
<body>
</body>
</html>
参考资料
1111的博客.1111的博客[引用时间2018-1-24]
<html>
<head>
<meta charset="UTF-8"/>
<title></title>
<script type="text/javascript">
var a=["America","Greece","Britain","Canada","China","Egypt"];
var count=0;
document.write("在以下字符中:<br/>");
for(var i in a){
document.write(a[i]+"<br/>");
if(a[i].indexOf("a")!=-1||a[i].indexOf("A")!=-1)
count++;
}
document.write("共有"+count+"个字符中串包含a或A。");
</script>
</head>
<body>
</body>
</html>
var sum_a=0;
var sum_A=0;
for(var i=0;i<str.length();i++){
if("a"==str.subString(0,i)){
sum_a++;
}
if("A"==str.subString(0,i)){
sum_A++;
}
}
alter("输出")
2016-05-06
var str = "a1Ab2c3dqq4";
alert(str.match(reg).length);
var str="aAsdadA";
var sum_a=0;
var sum_A=0;
for(var i=0;i<str.length();i++){
if("a"==str.subString(0,i)){
sum_a++;
}
if("A"==str.subString(0,i)){
sum_A++;
}
}
alter("输出")
var str = new Array("America","Greece","Britain","Canada","China","Egypt");
var sum_a = 0;
var sum_A = 0;
for(var i=0; i<str.length;i++){
for(var j=0; j<str[i].length;j++){
if(str[i].charAt(j)=="a"){
sum_a++;
}
if(str[i].charAt(j)=="A"){
sum_A++;
}
}
}
alert("统计a的个数:"+sum_a+ "<br />" + "统计A的个数"+sum_A);
</script>