本文介绍了比较JavaScript中的两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi .. 我正在尝试比较两个字符串以进行排序.我正在通过使用=,>直接比较此字符串.和< operator.i在某些情况下出现问题,例如: if(abc12<abc2) >它返回true,但实际我想要abc12字符串必须更大. 我的代码是
hi.. I am trying to compare two string for sorting purpose.i am comparing this string directly by using =,> and < operator.i am getting problem in some case eg: if(abc12<abc2) > it returns true but actual i want abc12 string must be greater. my code is
str1=abc12; str2=abc2; if(str1<str2) { alert('str1'); } else { alert('str2'); }我得到的输出为str1.其实我要str2. 提供解决方案.
i got output as str1. Actually i want str2. Provide a solution.
推荐答案www.java2s/Tutorial/JavaScript/0120__String/Comparetwostrings.htm [ ^ ] www.zimbio/Web+Design/articles/472/Javascript + Compare + Two + Input + Strings [ ^ ] javascript.about/od/decisionmaking/a/des02.htm [ ^ ] www.java2s/Tutorial/JavaScript/0120__String/Comparetwostrings.htm[^] www.zimbio/Web+Design/articles/472/Javascript+Compare+Two+Input+Strings[^] javascript.about/od/decisionmaking/a/des02.htm[^]
首先,您应该查看函数 string.localCompare [ ^ ]并使其正常工作,您还必须比较字符串长度 first of all you should look at the function string.localCompare[^] as well as to make it work properly you will have to compare string length too
hiii, hiii, function myFunction() { var x="abc123"; var y="abc122" if((x.localeCompare(y)<0 && x.length<=y.length)) { alert(y); } else { alert(x); } }
希望它能正常工作
hope this will work fine
更多推荐
比较JavaScript中的两个字符串
发布评论