前言
在客户端如Android的时候会涉及到版本的比较,版本号示例
1.0.0
1.0.a1
代码实现
public class Client {
private static int versionCompare(String version1, String version2) {
String[] versionArr1 = version1.split("\.");
String[] versionArr2 = version2.split("\.");
int minLen = Math.min(versionArr1.length, versionArr2.length);
int diff = 0;
for (int i = 0; i < minLen; i++) {
String v1 = versionArr1[i];
String v2 = versionArr2[i];
diff = v1.length() - v2.length();
if (diff == 0) {
diff = v1.compareTo(v2);
}
if (diff != 0) {
break;
}
}
diff = (diff != 0) ? diff : (versionArr1.length - versionArr2.length);
return diff;
}
public static void main(String[] args) {
System.out.println(versionCompare("1.2", "1.3"));
System.out.println(versionCompare("1.2", "1.1"));
System.out.println(versionCompare("1.2", "1.1.4"));
System.out.println(versionCompare("9.9", "10.8"));
System.out.println(versionCompare("9.a", "9.b"));
}
}
根据点号分割,然后一级一级比较,主版本和主版本号比较,次版本号和次版本号比较。