如何解决如何使用javascript比较字符串“ 17.4.0.50”和“ 3.1.0.114”?
我试图比较以下字符串。但我无法获得正确答案。
var a = "17.4.0.50";
var b = "3.1.0.114";
if(a>b){
alert("a is greater than b");
}
else{
alert("b is greater than a");
}
得到的结果是“ b大于a”。
有人可以帮助我获得正确的结果吗?
预先感谢
解决方法
您似乎正在尝试比较版本。这就是我的建议
//Need to compute the common minimum patch version place
var numPatchIdentifiersArray = new Array();
var aPatches = a.split(".")
var bPatches = b.split(".")
numPatchIdentifiersArray.push(aPatches.length);
numPatchIdentifiersArray.push(bPatches.length);
var minNumPatchIdentifiers = min(numDecimalsArray);
for (var index = 0; index < minNumPatchIdentifiers; index++) {
if (parseInt(aPatches[index]) > parseInt(bPatches[index])) {
console.log("a > b");
break;
} else if (parseInt(aPatches[index]) < parseInt(bPatches[index])) {
console.log("a < b");
break;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。