本文介绍了javascript中的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我必须比较两个字符串。如果他们的情况不同,也会发出错误警报。为此,我的代码如下: var a,b; a = document .f1.t1.value; b = document .f1.t2.value; bb = ' '; cc = ' '; for (i = 0 ; i< a.length; i ++) { for (j = 0 ; j< b.length; j ++ ) { if (b.charAt(j)> = 65 && b.charAt(j)< = 90 ) { bb = bb + b.charAt(j); } else bb = bb + b.charAt(j); } 如果(a.charAt(i)> = 65 && a.charAt(i)< = 90 ) { cc = cc + a.charAt(i ); } else cc = cc + a.charAt(i); } 如果(bb == cc) { alert( 字符串相等); } else alert( 字符串不相等); < / script> < / head > < body> < form name = f1> < input type = text name = t1> < input type = text name = t2> < input type = button value = ok onclick = REV()> < / 表格 > < / body > < / html > 但是没有执行。我错了吗?解决方案 如果条件是 true ,那么你最终会做同样的事情(什么?!),如果它是 false 。 这里说明基于单个字符的字符串相等。 i have to compare two strings. if their cases are dissimilar than also it alerts a error. for this my code is as follows:var a,b;a = document.f1.t1.value;b = document.f1.t2.value;bb = '';cc = '';for(i=0;i<a.length;i++){ for(j=0;j<b.length;j++) { if(b.charAt(j) >=65 && b.charAt(j) <= 90) { bb = bb + b.charAt(j); } else bb = bb +b.charAt(j); } if (a.charAt(i) > =65 && a.charAt(i) <= 90) { cc = cc + a.charAt(i); } else cc = cc + a.charAt(i); } if (bb == cc) { alert("strings are equal"); } else alert("strings are unequal");</script></head><body> <form name = "f1"> <input type ="text" name ="t1"> <input type ="text" name ="t2"> <input type="button" value="ok" onclick="rev()"> </form></body></html>but is is not executing.where i am wrong? 解决方案 Here you end up doing the same thing (what?!) either if the condition is true and if it is false.Here you are stating the strings are equal based on a single character equality. 这篇关于javascript中的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 19:14