Possible Duplicate:
Matching a “.” in java
I have a String 1.2.4 which i want to split and get 2 to compare it with another String that i get.
I am trying to do the following
StringTokenizer old_ver = new StringTokenizer(oldVersion, ".");
int oldVer = Integer.parseInt(old_ver.nextToken());
oldVer = Integer.parseInt(old_ver.nextToken());
StringTokenizer new_ver = new StringTokenizer(newVersion, ".");
int newVer = Integer.parseInt(new_ver.nextToken());
newVer = Integer.parseInt(new_ver.nextToken());
if (oldVer == newVer) {
return true;
}
IS there a better way to do it using .Split()
In your code, 1.2.1 would be “compatible” with 3.2.0, and this looks like a serious problem, regardless if you use
String.splitor not. Also, you do not need to parse version numbers into integers as you only compare for equality.You can surely do with
String.splitas well:The
String.split()version seems slightly shorter but but for me it looks slightly more difficult to read because:It may pay to have some version comparator that first compares major, then intermediate and then minor parts of the version number the way that we could have the tests like “1.2.4 or later”.