Java String Split splitVersion(String version)

Here you can find the source of splitVersion(String version)

Description

split Version

License

Open Source License

Declaration

public static int[] splitVersion(String version) 

Method Source Code

//package com.java2s;
/**/*from  w w w .  j  a  va2s.co m*/
 *  Copyright 2008 Marvin Herman Froeder
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 *
 */

import java.util.Arrays;

public class Main {
    public static int[] splitVersion(String version) {
        if (version == null || version.trim().equals("")) {
            return new int[0];
        }

        int endIndex = version.indexOf('-');
        if (endIndex != -1) {
            version = version.substring(0, endIndex);
        }

        String[] versionsStr = version.split("\\.");
        int[] versions = new int[versionsStr.length];

        for (int i = 0; i < versionsStr.length; i++) {
            try {
                versions[i] = new Integer(versionsStr[i]);
            } catch (NumberFormatException e) {
                versions[i] = 0;
            }
        }

        return versions;
    }

    public static int[] splitVersion(String version, int size) {
        int[] versions = splitVersion(version);

        if (versions.length != size) {
            int[] temp = new int[size];
            Arrays.fill(temp, 0);
            System.arraycopy(versions, 0, temp, 0, Math.min(versions.length, size));
            versions = temp;
        }

        return versions;
    }
}

Related

  1. splitToTopLevelJsonObjects(String s)
  2. splitTypeArguments(final String nestedTypes)
  3. splitTypes(String fullName)
  4. splitValues(String strValues)
  5. splitValues(String value)
  6. splitWithoutEscaped(String str, String sep)
  7. splitWS(String s, boolean decode)
  8. splitws(String val)
  9. stringSplit(String s)