从字符串中获取一个多十进制数

时间:2015-12-05 09:37:33

标签: java numbers

对于我的一个项目,我想从一个具有多个小数的字符串中获取一个版本,是否可以将其转换为多个小数点的双倍,或者它是不可能的。我想用它来查看它是否比前一个更多,它也有多个小数。

目前我正在使用的是 if (!vers.equalsIgnoreCase(plugin.getDescription().getVersion())) { // Do stuff 但是我想这样做我能做到 if (vers > plugin.getDescription().getVersion()) { // do stuff  vers等于1.0.1,plugin.getDescription()。getVersion()等于1.0.2

谢谢!

5 个答案:

答案 0 :(得分:0)

String[] numbers = version.split(".");
String[] numbers2 = version2.split(".");
int index = numbers.length-1;
while(numbers[index] != "."){
     index--;
}
String lastnumber = version.substring(index+1, numbers.length];
index = numbers2.length-1;
while(numbers2[index] != "."){
     index--;
}
String lastnumber2 = version.substring(index+1, numbers2.length];
if(lastnumber > lastnumber2){
    //later version
}else{
   //use the same thing to check the other numbers
}

答案 1 :(得分:0)

您似乎想要比较版本。或者根据一些字符串表示的版本做出决定。如果plugin.getDescription().getVersion()String,那么您应该能够使用简单的String比较来建立版本之间的顺序。这样的事情应该有效:

String pluginVersion=plugin.getDescription().getVersion();
if (ensureValidVersion(pluginVersion) 
     && compareVersions(vers,pluginVersion)>0) {
  // do staff is vers is greater then plugin version
} 

ensureValidVersion方法将验证您是否具有有效的版本号表示。 compareVersions将对每个版本的子组件进行比较。

答案 2 :(得分:0)

如果你假设所有部分都是数字,你可以用这种方式实现。

public static int compareVersions(String vers1, String vers2) {
    String[] v1 = vers1.split("\\.");
    String[] v2 = vers2.split("\\.");
    for (int i = 0; i < v1.length && i < v2.length; i++) {
        int i1 = Integer.parseInt(v1[i]);
        int i2 = Integer.parseInt(v2[i]);
        int cmp = Integer.compare(i1, i2);
        if (cmp != 0)
            return cmp;
    }
    return Integer.compare(v1.length, v2.length);
}

System.out.println(compareVersions("1.0.1", "1.0.2"));
System.out.println(compareVersions("1.0.1", "1.0"));
System.out.println(compareVersions("1.0.2", "1.0.10"));

打印

-1
1
-1

更复杂的版本支持版本

内的字母
public static int compareVersions(String vers1, String vers2) {
    String[] v1 = vers1.split("\\.");
    String[] v2 = vers2.split("\\.");
    for (int i = 0; i < v1.length && i < v2.length; i++) {
        String [] w1 = v1[i].split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
        String [] w2 = v2[i].split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
        for(int j=0;j<w1.length&&j<w2.length;j++) {
            try {
                int i1 = Integer.parseInt(w1[j]);
                int i2 = 0;
                try {
                    i2 = Integer.parseInt(w2[j]);
                } catch (NumberFormatException e) {
                    return -1;
                }
                int cmp = Integer.compare(i1, i2);
                if (cmp != 0)
                    return cmp;
            } catch (NumberFormatException e) {
                try {
                    Integer.parseInt(w2[j]);
                    return +1;
                } catch (NumberFormatException e1) {
                    int cmp = w1[j].compareTo(w2[j]);
                    if (cmp != 0)
                        return cmp;
                }
            }
        }
        int cmp = Integer.compare(w1.length, w2.length);
        if (cmp != 0)
            return cmp;
    }
    return Integer.compare(v1.length, v2.length);
}

System.out.println(compareVersions("1.0.2", "1.0.2a"));
System.out.println(compareVersions("1.0.2b", "1.0.2a"));
System.out.println(compareVersions("1.8.0_66", "1.8.0_65"));
System.out.println(compareVersions("1.7.0_79", "1.8.0_65"));

打印

-1
1
1
-1

答案 3 :(得分:0)

假设您有以下格式的版本号:x.y.z
您可以使用Viacheslav Vedenin建议的类似方法:

String versionNumber = "1.3.45";
String[] singleParts = versionNumbers.split(".");

int[] versionParts = new int[singleParts.length];
for(int i=0; i<singleParts.length; i++) {
    versionParts[i] = Integer.parseInt(singleParts[i]);
}

现在您拥有版本号的单个部分数组。要将其与之前的版本进行比较,您可以执行以下操作:

public static boolean isGreater(int[] firstVersion, int[] secondVersion) {
    if(secondVersion.length > firstVersion.length) {
        return false;
    }else {
        if(firstVersion.length > secondVersion.length) {
            return true;
        }else {
            for(int k=0; k< firstVersion.length; k++) {
                int v1 = firstVersion[k];
                int v2 = secondVersion[k];

                if(v1 < v2) {
                    return true;
                }
            }
            return false;
        }
    }
}

答案 4 :(得分:0)

如果要使用相等/不等运算符(==<><=>=)来比较版本,两个选项。

  1. 使用支持运算符重载的C ++语言
  2. major.minor.build中为每个字符串设置长度限制,并在比较它们之前将每个版本转换为整数。例如,如果每个限制为3(即您可以拥有的最长版本为abc.def.ghi),则可以使用build + minor * 10^3 + major * 10^6
  3. 或者,你可以实现Comparable<Version>并拥有一个很好的OOP解决方案。

    public class Example {
        static class Version implements Comparable<Version> {
            private int major;
            private int minor;
            private int build;
    
            public Version(String s) {
                final String[] split = s.split("\\.");
                major = Integer.parseInt(split[0]);
                minor = Integer.parseInt(split[1]);
                build = Integer.parseInt(split[2]);
            }
    
            public int getMajor() {
                return major;
            }
    
            public int getMinor() {
                return minor;
            }
    
            public int getBuild() {
                return build;
            }
    
            @Override
            public int compareTo(Version v) {
                if (getMajor() < v.getMajor()) {
                    return -1;
                } else if (getMajor() > v.getMajor()) {
                    return 1;
                } else {
                    if (getMinor() < v.getMinor()) {
                        return -1;
                    } else if (getMinor() > v.getMinor()) {
                        return 1;
                    } else {
                        if (getBuild() < v.getBuild()) {
                            return -1;
                        } else if (getBuild() > v.getBuild()) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            String s1 = "1.0.1";
            String s2 = "1.0.2";
            compare(s1, s2);
            compare(s1, s1);
            compare(s2, s2);
            compare(s2, s1);
        }
    
        private static void compare(String s1, String s2) {
            Version v1 = new Version(s1);
            Version v2 = new Version(s2);
            final int compareTo = v1.compareTo(v2);
            if (compareTo == -1) {
                System.out.println(s1 + " was released before " + s2);
            } else if (compareTo == 0) {
                System.out.println(s1 + " is the same as " + s2);
            } else {
                System.out.println(s1 + " was released after " + s2);
            }
        }
    }
    

    输出:

    1.0.1 was released before 1.0.2
    1.0.1 is the same as 1.0.1
    1.0.2 is the same as 1.0.2
    1.0.2 was released after 1.0.1