字符串匹配度计算

<script>
alert(getPercent('我是中国人', '她不是中国人,是《日本人》'));

function getPercent(s1, s2) {
  var str1 = '', str2 = '', n = 0, sum = 0;
    
  if (s1.length > s2.length) str1 = s2, str2 = s1; else str1 = s1, str2 = s2;
  
  for (var i = 0, len = str1.length; i < len; i++) if (str2.indexOf(str1.charAt(i), n) != -1) sum++, n = str2.indexOf(str1.charAt(i));
  
  return (sum / str2.length * 100).toFixed(2);
}
</script>

 

posted @ 2014-03-11 12:46  baiezone  阅读(548)  评论(0编辑  收藏  举报