问题描述
比如我想实现传递字符‘11’ 在数据中搜索{‘112’,‘121’,‘221’,‘211’,‘121’}有多少个数据是有两个11的 问题补充:这里我应该得到4个
解决方案
<script type="text/javascript">$(document).ready(function(){var str = "{'112','121','221','211','121'}";str = str.replace('{','[').replace('}',']');var arr = eval(str);var reg = /^.*1+.*1+.*$/;var count = 0;$.each(arr,function(s){if(reg.exec(arr[s])){count++;}});alert(count);});</script>
解决方案二:
将对应字符串替换成"",比较替换前和替换后的长度即可
解决方案三:
在 wanghuanqiu 兄的代码上扩展了下! function huoqu(){ var count=0; var ayy =['112','1211','221','211','121']; for(var i=0 ;i<ayy.length;i++){ var index=ayy[i].length-ayy[i].replace(/[1]/g,'').length; alert(index) if(index==2 && ayy[i].indexOf("1")!=ayy[i].lastIndexOf("1")){ count ++; } } alert(count); }
解决方案四:
没用过jquery,纯jsfunction getString(ayy){var count=0;for(var i=0 ;i<ayy.length;i++){ var index = ayy[i].indexOf("1"); var lastIndex = ayy[i].lastIndexOf("1"); if((index != lastIndex ) && (ayy[i].substring(index+1,lastIndex).indexOf("1") == -1)){ count ++;}}return count;}var arry =['112','1211','221','211','121'];alert(getString(arry));
解决方案五:
function getString(ayy){var count=0;for(var i=0 ;i<ayy.length;i++){ if(ayy[i].indexOf("1")!=ayy[i].lastIndexOf("1")){ count ++;}}return count;}var arry =['112','121','221','211','121'];alert(getString(arry));
解决方案六:
public getString(String[] ayy){int count=0;for(int i=0 ;i<ayy.length;i++){ if(ayy[i].indexOf("1")!==ayy[i].lastIndexOf("i")){ count ++;}}System.out.println("共有"11"+count);}