文字列-切り取り文字列指定長さ+文字列が数値に変換可能かどうかを判断


import org.apache.commons.lang3.StringUtils;

/**
 * Created by weijun.nie on 2017/9/13.
 */
public class NiewjStringUtils {

    /**
     *          targetString    len  ,     len    ;    ,    .
     *
     * @param targetString       
     * @param len               
     * @return
     */
    public static String subLen(String targetString, int len) {
        if (StringUtils.isBlank(targetString) || targetString.length() <= len) {
            return targetString;
        }
        return targetString.substring(0, len);
    }

    /**
     *               
     *
     * @param number
     * @return
     */
    public static boolean isNumber(String number) {
        // 1.   ,false
        if (StringUtils.isBlank(number))
            return false;

        // 2.            ,     
        if (number.startsWith("-") || number.startsWith("+")) {
            number = number.substring(1);
        }

        /**
         * 3.     :
         *     ,          ;
         *    ,             
         */
        int index = number.indexOf(".");
        if (index < 0) {
            return StringUtils.isNumeric(number);
        } else {
            String num1 = number.substring(0, index);
            String num2 = number.substring(index + 1);

            return StringUtils.isNumeric(num1) && StringUtils.isNumeric(num2);
        }
    }

    public static void main(String[] args) {

    }
}
  • maven依存:
    
        org.apache.commons
        commons-lang3