JavaScript 代码实现功能

来源:百度知道 编辑:UC知道 时间:2024/06/18 18:11:58
1、输入一串字符串,实现查找功能。
要求:首先输入一串字符串,再输入要查找的字符串。如有,逐个输出弹出位置号,最后统计总共出现了多少次。

<script>
var str = 'asd8as8dsa98d98';
var me = 'as';
var len = me.length;
var tmp = '';
var times = '';
for (i=0,l=str.length;i<l;i++){
tmp = str.substr(i,len);
if ( tmp != me ) continue;
times += (times==''?'':',') + i + ':' + tmp;
}
alert(times);
alert(times.split(',').length);

</script>

逐字符循环被查找串
截取与被查串等长的字符

<script language="javascript">
function contains(stringA,str,op_type)
{
/*
* N,N,N,N,N represents you set op_type = false , and we have different items returned
* N>0 represents contains
* N==0 represents no item contains
* N==-1 represents <str> is longger than <stringA>
* N==-2 represents <str> 's length is 0
* N==-3 represents special no item contains
* N==-4 when you set the op