2017ama全美音乐奖:javascript中用什么方法查找某一字符串中某个字符出现的次数??

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/14 09:15:17
javascript中用什么方法查找某一字符串中某个字符出现的次数??

比如:
要查找 aaa|bbb|ccc|ddd|eee|fff|ggg
中|出现的次数,怎么操作??

<script language="javascript">
function check()
{
var str=document.getElementById("input1").value
obj=str.match(/\|/g)
if(obj!=null)
{
alert(obj.length)
}
}
</script>
<input id="input1" type="text" value="">
<input type="button" value="确定" onclick="check()">

String str="aaa|bbb|ccc|ddd|eee|fff|ggg";
int count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='|'){
count++;
}
}
System.out.print(count);

可以使用正则表达式