简体   繁体   English

你如何比较 Java 中的两个版本字符串?

[英]How do you compare two version Strings in Java?

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习惯用法? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的 String compareTo,因为我还不知道最大发布点数是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并有以下成立:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

The most simple solution is using string compare.最简单的解决方案是使用字符串比较。 Remove the unwanded dots or letters in case the version is like v1.0.1Beta using Regex("[^0-9]") and you get for versions like 1.0 and 1.0.1:删除不需要的点或字母,以防版本类似于 v1.0.1Beta 使用 Regex("[^0-9]") 并且您得到 1.0 和 1.0.1 等版本:

String ver1 = "10";
String ver2 = "101";
    
int res = ver1.compareTo(ver2);
System.out.println("res: "+res);

If ver1 is lexicographically less than ver2 , a negative number will be returned, 0 if equal or a positive number if ver1 is greater.如果ver1在字典上小于ver2 ,则将返回一个负数,如果相等则返回 0,如果ver1大于则返回正数。

The compareTo() method compares two strings lexicographically. compareTo() 方法按字典顺序比较两个字符串。

The comparison is based on the Unicode value of each character in the strings.比较基于字符串中每个字符的 Unicode 值。

The method returns 0 if the string is equal to the other string.如果字符串等于另一个字符串,则该方法返回 0。 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).如果字符串小于另一个字符串(更少字符),则返回小于 0 的值;如果字符串大于另一个字符串(更多字符),则返回大于 0 的值。

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

Is there a standard idiom for comparing version numbers?是否有用于比较版本号的标准习语? I can't just use a straight String compareTo because I don't know yet what the maximum number of point releases there will be.我不能只使用直接的String compareTo,因为我还不知道最大的点释放数将是多少。 I need to compare the versions and have the following hold true:我需要比较版本,并满足以下条件:

1.0 < 1.1
1.0.1 < 1.1
1.9 < 1.10

My java Solution 我的java解决方案

 public int compareVersion(String version1, String version2) {

    String[] first = version1.split("\\.");
    String[] second = version2.split("\\.");

    int len = first.length<=second.length? first.length:second.length;

    // the loop runs for whichever is the short version of two strings
    for(int i=0;i<len;i++){
      int firstInt = Integer.parseInt(first[i]);
      int secondInt = Integer.parseInt(second[i]);
        if(firstInt<secondInt){
            return -1;
        }
        else if(firstInt>secondInt){
            return 1;
        }
    }

    // below two condition check if the length are the not the same

    //if first string length is short then start from after first string length and compare it with second string value. second string value is not zero that means it is greater.
    if(first.length<second.length){
        for(int i=first.length;i<second.length;i++){
            int secondInt = Integer.parseInt(second[i]);
            if(secondInt!=0){
                return -1;
            }
        }
    }
    // similar logic as above just that first length is grater this time.
     else if(first.length>second.length){
        for(int i=second.length;i<first.length;i++){
            int firstInt = Integer.parseInt(first[i]);
            if(firstInt!=0){
                return 1;
            }
        }        
    }

    // return 0 if both string value is the same
    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM