Possible Duplicate:
How do you compare two version Strings in Java?
I've 2 strings which contains version information as shown below:
str1 = "1.2" str2 = "1.1.2"
Now, can any one tell me the efficient way to compare these versions inside strings in Java & return 0 , if they're equal, -1, if str1 < str2 & 1 if str1>str2.
We can compare String in Java on the basis of content and reference. It is used in authentication (by equals() method), sorting (by compareTo() method), reference matching (by == operator) etc. There are three ways to compare String in Java: By Using equals() Method.
The right way of comparing String in Java is to either use equals(), equalsIgnoreCase(), or compareTo() method. You should use equals() method to check if two String contains exactly same characters in same order. It returns true if two String are equal or false if unequal.
Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.
Java String compareTo() Method The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).
Requires commons-lang3-3.8.1.jar for string operations.
/** * Compares two version strings. * * Use this instead of String.compareTo() for a non-lexicographical * comparison that works for version strings. e.g. "1.10".compareTo("1.6"). * * @param v1 a string of alpha numerals separated by decimal points. * @param v2 a string of alpha numerals separated by decimal points. * @return The result is 1 if v1 is greater than v2. * The result is 2 if v2 is greater than v1. * The result is -1 if the version format is unrecognized. * The result is zero if the strings are equal. */ public int VersionCompare(String v1,String v2) { int v1Len=StringUtils.countMatches(v1,"."); int v2Len=StringUtils.countMatches(v2,"."); if(v1Len!=v2Len) { int count=Math.abs(v1Len-v2Len); if(v1Len>v2Len) for(int i=1;i<=count;i++) v2+=".0"; else for(int i=1;i<=count;i++) v1+=".0"; } if(v1.equals(v2)) return 0; String[] v1Str=StringUtils.split(v1, "."); String[] v2Str=StringUtils.split(v2, "."); for(int i=0;i<v1Str.length;i++) { String str1="",str2=""; for (char c : v1Str[i].toCharArray()) { if(Character.isLetter(c)) { int u=c-'a'+1; if(u<10) str1+=String.valueOf("0"+u); else str1+=String.valueOf(u); } else str1+=String.valueOf(c); } for (char c : v2Str[i].toCharArray()) { if(Character.isLetter(c)) { int u=c-'a'+1; if(u<10) str2+=String.valueOf("0"+u); else str2+=String.valueOf(u); } else str2+=String.valueOf(c); } v1Str[i]="1"+str1; v2Str[i]="1"+str2; int num1=Integer.parseInt(v1Str[i]); int num2=Integer.parseInt(v2Str[i]); if(num1!=num2) { if(num1>num2) return 1; else return 2; } } return -1; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With