赞
踩
/*
* 版本号比较
*
* @param v1
* @param v2
* @return 0代表相等,1代表左边大,-1代表右边大
*/
public static int compareVersion(String v1, String v2) {
if (v1.equals(v2)) {
return 0;
}
String[] version1Array = v1.split("[._]");
String[] version2Array = v2.split("[._]");
int index = 0;
int minLen = Math.min(version1Array.length, version2Array.length);
long diff = 0;
while (index < minLen
&& (diff = Long.parseLong(version1Array[index])
- Long.parseLong(version2Array[index])) == 0) {
index++;
}
if (diff == 0) {
for (int i = index; i < version1Array.length; i++) {
if (Long.parseLong(version1Array[i]) > 0) {
return 1;
}
}
for (int i = index; i < version2Array.length; i++) {
if (Long.parseLong(version2Array[i]) > 0) {
return -1;
}
}
return 0;
} else {
return diff > 0 ? 1 : -1;
}
}
原文作者:JackieZhengChina
原文地址:Java 比较两个版本号的大小 (通用)_JackieZhengChina的博客-CSDN博客/**
原文链接:https://blog.csdn.net/admans/article/details/81865652
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。