我必须比较两个字符串,因为str1包含str2的百分比是多少。
str1="Hello wolrd"
str2="hello"意味着distance(str1, str2)应该返回50%..就像这样。
发布于 2012-04-16 14:50:40
请尝试使用以下代码。
<script>
var str1 = 'helloworld';
var str2 = 'hello';
var percent = 0;
var newstr = str1.search(str2);
if(newstr != -1){
percent = (str2.length * 100)/str1.length;
alert(percent+'%');
}else{
alert(percent+'%');
}
</script>如果我理解你的问题,我相信这会对你有帮助。
谢谢。
https://stackoverflow.com/questions/10169612
复制相似问题