How can I compare two Version number strings?
For example: 3.1.1 and 3.1.2.5.4
Now I need to find out if 3.1.2.5.4 is higher than 3.1.1 but I don't know how to do this. Can anybody help me?
Thanks in advance!
Sample Code :
NSString* v1 = @"3.1.1";
NSString* v2 = @"3.1.2.5.4";
if ([v1 compare:v2 options:NSNumericSearch] == NSOrderedDescending) {
NSLog(@"%@ is greater than %@",v1,v2);
}
From the Apple Documentation for Comparing and sorting strings.
Yes, you can compare the versions, please refer the code below:
public class Comparision {
string ver1, ver2;
public static void main(String args[]){
string ver1Split[] = ver1.split('.');
string ver2Split[] = ver2.split('.');
for (int i = 0; i < ver1Split.length; ++i) {
if (ver2Split == i) {
return ver1 + " is larger";
}
if (ver1Split[i] == ver2Split[i]) {
continue;
}
else if (ver1Split[i] > ver1Split[i]) {
return ver1 + " is larger";
}
else {
return ver2 + " is larger";
}
if (ver1Split.length != ver2Split.length) {
return ver2 + " is larger";
}
return "versions are equal";
}
}
This is a general logical approach in Java using split.
String[] tokens = versionString.split("\\.");
and then just compare the version numbers or Strings in the array.
How can I compare two Version number strings?
For example: 3.1.1 and 3.1.2.5.4
Now I need to find out if 3.1.2.5.4 is higher than 3.1.1 but I don't know how to do this. Can anybody help me?
Thanks in advance!
Sample Code :
NSString* v1 = @"3.1.1";
NSString* v2 = @"3.1.2.5.4";
if ([v1 compare:v2 options:NSNumericSearch] == NSOrderedDescending) {
NSLog(@"%@ is greater than %@",v1,v2);
}
From the Apple Documentation for Comparing and sorting strings.
Yes, you can compare the versions, please refer the code below:
public class Comparision {
string ver1, ver2;
public static void main(String args[]){
string ver1Split[] = ver1.split('.');
string ver2Split[] = ver2.split('.');
for (int i = 0; i < ver1Split.length; ++i) {
if (ver2Split == i) {
return ver1 + " is larger";
}
if (ver1Split[i] == ver2Split[i]) {
continue;
}
else if (ver1Split[i] > ver1Split[i]) {
return ver1 + " is larger";
}
else {
return ver2 + " is larger";
}
if (ver1Split.length != ver2Split.length) {
return ver2 + " is larger";
}
return "versions are equal";
}
}
This is a general logical approach in Java using split.
String[] tokens = versionString.split("\\.");
and then just compare the version numbers or Strings in the array.
0 commentaires:
Enregistrer un commentaire