Java Version Convert To versionStringToNumber(String versionStr)

Here you can find the source of versionStringToNumber(String versionStr)

Description

Converts a version string, such as "1.12.5 alpha" to a number.

License

Open Source License

Parameter

Parameter Description
versionStr A string of type "X.Y.Z suffix". The suffix is ignored, Y and Z are optional. Y and Z must not be larger than 99.

Return

A number. For the example string "1.12.5 alpha", this is 1 * 100 * 100 + 12 * 100 + 5;

Declaration

public static int versionStringToNumber(String versionStr) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w  ww.ja va  2s  .c  o  m
     * Converts a version string, such as "1.12.5 alpha" to a number.
     * @param versionStr A string of type "X.Y.Z suffix". The suffix is ignored,
     * Y and Z are optional. Y and Z must not be larger than 99.
     * @return A number. For the example string "1.12.5 alpha", this is 
     * 1 * 100 * 100 + 12 * 100 + 5;
     */
    public static int versionStringToNumber(String versionStr) {
        // remove " alpha" at the end of the string
        String[] strs = versionStr.trim().split(" ");

        // split into numbers
        strs = strs[0].split(".");

        // the following could be a little more elegant...
        if (strs.length == 1) {
            return Integer.parseInt(strs[0]) * 100 * 100;
        }

        if (strs.length == 2) {
            if (Integer.parseInt(strs[1]) > 99) {
                throw new IllegalArgumentException("subversion too large");
            }
            return Integer.parseInt(strs[0]) * 100 * 100 + Integer.parseInt(strs[1]) * 100;
        }

        if (strs.length == 3) {
            if (Integer.parseInt(strs[1]) > 99 || Integer.parseInt(strs[2]) > 99) {
                throw new IllegalArgumentException("subversion too large");
            }
            return Integer.parseInt(strs[0]) * 100 * 100 + Integer.parseInt(strs[1]) * 100
                    + Integer.parseInt(strs[2]);
        }

        return 0;
    }
}

Related

  1. versionStringToInt(String version)
  2. versionStringToInt(String version)
  3. versionToCode(String version)
  4. versionToSegments(String versionString, int idx, int i)
  5. versionToString(int version)
  6. versiontostring(int vv)