zoukankan      html  css  js  c++  java
  • Java 数字 字符串 简单操作

    数字类型验证:不是数字 或 是负数 返回 true

    public static boolean isNotNumOrNegative(Number num) {
        if (num == null) {
            return true;
        }
        if (num instanceof Integer) {
            int v = num.intValue();
            return v < 0;
        }
        if (num instanceof Long) {
            long v = num.longValue();
            return v < 0;
        }
        if (num instanceof BigInteger) {
            BigInteger v = (BigInteger) num;
            return v.compareTo(BigInteger.valueOf(0L)) < 0;
        }
        if (num instanceof BigDecimal) {
            BigDecimal v = (BigDecimal) num;
            return v.compareTo(BigDecimal.valueOf(0L)) < 0;
        }
        return false;
    }
    
    public static boolean isNotNumOrNegative(Number... nums) {
        for (Number num : nums) {
            if (isNotNumOrNegative(num)) {
                return true;
            }
        }
        return false;
    }

    字符串验证:

    public static boolean isNullOrEmpty(String val) {
        return val == null || "".equals(val.trim());
    }
    
    public static boolean isNullOrEmpty(String... vals) {
        for (String val : vals) {
            if (isNullOrEmpty(val)) {
                return true;
            }
        }
        return false;
    }

    生成6位随机字符:

    public static String getRandomChar() {
        StringBuilder chars = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int index = new Random().nextInt(26) + 65;
            chars.append((char) index);
        }
        return chars.toString();
    }

    生成6位验证码:

    String.format("%06d", (int) (Math.random() * 1000000))
    知止而后有定;定而后能静;静而后能安;安而后能虑;虑而后能得。
  • 相关阅读:
    【iOS】找工作的面试题集锦
    APP项目优化--启动速度优化篇
    【Swift】Timer定时器到底准不准确?
    leetcode刷题 495~
    leetcode刷题 464~
    leetcode刷题 441~
    leetcode刷题 420~
    leetcode刷题 396~
    leetcode刷题 373~
    leetcode刷题 307~
  • 原文地址:https://www.cnblogs.com/SamNicole1809/p/15413448.html
Copyright © 2011-2022 走看看