版本更新比較常用的接口,字元串解析,不是很難,但沒必須重複造輪子,儲存一份網上搜到的實作:
/**
* 比較版本号的大小,前者大則傳回一個正數,後者大傳回一個負數,相等則傳回0
*
* @param version1
* @param version2
* @return
*/
public static int compareVersion(String version1, String version2) throws Exception {
if (version1 == null || version2 == null) {
throw new Exception("compareVersion error:illegal params.");
}
String[] versionArray1 = version1.split("\\.");//注意此處為正則比對,不能用".";
String[] versionArray2 = version2.split("\\.");
int idx = 0;
int minLength = Math.min(versionArray1.length, versionArray2.length);//取最小長度值
int diff = 0;
while (idx < minLength
&& (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比較長度
&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比較字元
++idx;
}
//如果已經分出大小,則直接傳回,如果未分出大小,則再比較位數,有子版本的為大;
diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
return diff;
}
判斷一個字元串是否符合mac位址規則:”78:DA:07:11:14:75”
private boolean stringIsMac(String val) {
String trueMacAddress = "([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}";
if (val.matches(trueMacAddress)) {
return true;
} else {
return false;
}
}