怎么用“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]
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='1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,7,24,25,26,27,28,29,30,31,32
';
var
arr=str.split(',');
执行之后数组arr里面就分别是1,2等