你的位置:首页 > Java教程

[Java教程]计算指定字符串出现次数插件


计算指定字符串出现次数插件:
有时候需要计算移一段字符串中指定字符串的出现次数,可能应用不是那么频繁。
本章节分享一段代码实例能够实现类似的功能,当然也可以根据实际需要进行扩展。
代码如下:

 

<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>数量统计工具-蚂蚁部落</title><script type="text/javascript" src="http://www.softwhy.com/mytest/jQuery/jquery-1.8.3.js"></script></head><body><p>源文件(将jQuery源码复制到下面的文本域):</p><p><textarea id="myjquery" style="width: 510px; height: 250px; padding: 2px;">..</textarea></p><p><input id="mybtn2" style="position: relative; left: 349px;" type="button" value="统计所有字符串出现次数" /></p><div id="myshow2" style="width: 500px; height: 250px; border: 1px dotted #8B8D72; overflow: auto; padding: 5px;"> </div><p></p><p><label for="myinput">字符串:<input id="myinput" type="text" /><input id="mybtn1" style="position: relative; left: 130px;" type="button" value="统计单个字符串出现次数" /></label></p><div id="myshow1" style="width: 500px; height: 100px; border: 1px dotted #8B8D72; overflow: auto; padding: 5px;"></div><script type="text/javascript">setTimeout(function(){  function buildRe(keywords)  {   var rObj = {};   if(keywords.constructor !== Array)   {    return;   }   keywords.forEach(function(it)   {    rObj[it] = RegExp(''+it, 'g');   }  )  return rObj; } function count(rObj, source, callback, sortType) {  var r,rarr,num,type,func,result = [];  var subCount=function(arr){   var i,re,num,resu;   i=num=0;   for(i;i<arr.length;i++)   {    re=arr[i];    while((resu=re.exec(source))!= null)    {     num++;    }   }   return num;  };  for(type in rObj)  {   rarr = rObj[type];   if(rarr.constructor !== Array)   {    rarr = [rarr];   }   num=subCount(rarr);   result.push({type: type, num: num});  }  if(typeof sortType!=='undefined')  {   if(sortType===0)   {    func=function(a,b)    {     return a.num-b.num;    };   }   else if(sortType===1)   {    func=function(a,b)    {     return b.num - a.num;    }  }   result.sort(func);  }  callback(result); } function main(keywords,source,callback,sortType) {  var rObj=keywords.constructor===Array?buildRe(keywords):keywords;  count(rObj, source, callback, sortType); } var rObj={  string: [/\'string\'/g, /\"string\"/g],  number: [/\'number\'/g, /\"number\"/g],  'boolean': [/\'boolean\'/g, /\"boolean\"/g],  object: [/\'object\'/g, /\"object\"/g],  'undefined': [/\'undefined\'/g, /\"undefined\"/g],  'function': [/\'function\'/g, /\"function\"/g],  'array': [/\'array\'/g, /\"array\"/g],  'ready': [/\'ready\'/g, /\"ready\"/g],  input: [/\'input\'/g, /\"input\"/g],  type: [/\'type\'/g, /\"type\"/g],  text: [/\'text\'/g, /\"text\"/g],  radio: [/\'radio\'/g, /\"radio\"/g],  checkbox: [/\'checkbox\'/g, /\"checkbox\"/g],  password: [/\'password\'/g, /\"password\"/g],  submit: [/\'submit\'/g, /\"submit\"/g],  button: [/\'button\'/g, /\"button\"/g],  id: [/\'id\'/g, /\"id\"/g],  div: [/\'div\'/g, /\"div\"/g],  body: [/\'body\'/g, /\"body\"/g],  html: [/\'html\'/g, /\"html\"/g],  HTML: [/\'HTML\'/g, /\"HTML\"/g],  parentNode: [/\'parentNode\'/g, /\"parentNode\"/g],  nextSibling: [/\'nextSibling\'/g, /\"nextSibling\"/g],  iframe: [/\'iframe\'/g, /\"iframe\"/g],  before: [/\'before\'/g, /\"before\"/g],  after: [/\'after\'/g, /\"after\"/g],  script: [/\'script\'/g, /\"script\"/g],  width: [/\'width\'/g, /\"width\"/g],  height: [/\'height\'/g, /\"height\"/g],  top: [/\'top\'/g, /\"top\"/g],  left: [/\'left\'/g, /\"left\"/g],  absolute: [/\'absolute\'/g, /\"absolute\"/g],  relative: [/\'relative\'/g, /\"relative\"/g],  'static': [/\'static\'/g, /\"static\"/g],  fixed: [/\'fixed\'/g, /\"fixed\"/g],  href: [/\'href\'/g, /\"href\"/g],  border: [/\'border\'/g, /\"border\"/g],  margin: [/\'margin\'/g, /\"margin\"/g],  marginTop: [/\'marginTop\'/g, /\"marginTop\"/g],  marginBottom: [/\'marginBottom\'/g, /\"marginBottom\"/g],  marginLeft: [/\'marginLeft\'/g, /\"marginLeft\"/g],  marginRight: [/\'marginRight\'/g, /\"marginRight\"/g],  padding: [/\'padding\'/g, /\"padding\"/g],  paddingTop: [/\'paddingTop\'/g, /\"paddingTop\"/g],  paddingLeft: [/\'paddingLeft\'/g, /\"paddingLeft\"/g],  paddingRight: [/\'paddingRight\'/g, /\"paddingRight\"/g],  display: [/\'display\'/g, /\"display\"/g],  olddisplay: [/\'olddisplay\'/g, /\"olddisplay\"/g],  none: [/\'none\'/g, /\"none\"/g],  hidden: [/\'hidden\'/g, /\"hidden\"/g],  inline: [/\'inline\'/g, /\"inline\"/g],  opacity: [/\'opacity\'/g, /\"opacity\"/g],  show: [/\'show\'/g, /\"show\"/g],  hide: [/\'hide\'/g, /\"hide\"/g],  toggle: [/\'toggle\'/g, /\"toggle\"/g],  json: [/\'json\'/g, /\"json\"/g],  success: [/\'success\'/g, /\"success\"/g],  fxshow: [/\'fxshow\'/g, /\"fxshow\"/g],  fx: [/\'fx\'/g, /\"fx\"/g],  '.run': [/\'.run\'/g, /\".run\"/g],  'http:': [/\'http:\'/g, /\"http:\"/g],  error: [/\'error\'/g, /\"error\"/g],  abort: [/\'abort\'/g, /\"abort\"/g],  GET: [/\'GET\'/g, /\"GET\"/g],  POST: [/\'POST\'/g, /\"POST\"/g],  get: [/\'get\'/g, /\"get\"/g],  filter: [/\'filter\'/g, /\"filter\"/g],  px: [/\'px\'/g, /\"px\"/g] }; function callback1(result) {  var obj=result[0];  var myshowEL=$('#myshow1');  myshowEL=myshowEL.empty();  myshowEL.append('<div>'+obj.type+':<span >'+obj.num+'</span></div>'); } function callback2(result) {  var myshowEL=$('#myshow2');  myshowEL=myshowEL.empty();  for(var i=0;i<result.length;i++)  {   var obj=result[i];   myshowEL.append('<div>"'+obj.type+'":<span >'+obj.num+'</span></div>');  } } $('#mybtn1').click(function(){  var $txtarea=$('#myjquery');  var source=$txtarea.val();  var val=$('#myinput').val();  if(val==''||val.length<2)  {   alert('至少两个字符');   return;  }  var keywords=[val];  main(keywords, source, callback1, 1) }); $('#mybtn2').click(function(){  var $txtarea = $('#myjquery');  var source = $txtarea.val();  main(rObj, source, callback2, 1); }); }, 1000); </script></body></html>

 

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=8729

更多内容可以参阅:http://www.softwhy.com/jquery/