【好用小方法】随机生成n个汉字/数字转汉字/字符串去重/list数组去重/获取2的幂次方/判断字符串是否包含标点符号/实体类复制相同属性对象/下划线转驼峰式/手机号码加密/map 取key值自动转格式

/**
 * 根据参数生成n个中文汉字
 *
 * @param num
 * @return
 */
public static List<String> getChaineseList(int num, List<String> aa) {
    if (num <= 0) return aa;
    String word = "";
    if (aa.size() > 0) {
        for (String s : aa) {
            word += s;
        }
    }
    try {
        for (int i = 0; i < num; i++) {
            String str = "";
            int hightPos, lowPos; // 定义高低位
            Random random = new Random();
            hightPos = (176 + Math.abs(random.nextInt(39)));//获取高位值
            lowPos = (161 + Math.abs(random.nextInt(93)));//获取低位值
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            str = new String(b, "GBk");//转成中文
            if (word.contains(str)) {
                i--;
                continue;
            }
            word += str + ",";
            aa.add(str);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return aa;
}

/**
 * 阿拉伯数字转中文数字
 *
 * @param
 * @return
 */

public static String ToConvtZH(Integer intInput) {
    String sd = ToCH(intInput);
    String sd2 = sd;
    if (intInput == 10) {
        sd2 = "十";
    }
    return sd2;
}

public static String ToCH(Integer intInput) {
    String si = intInput.toString();
    String sd = "";
    if (si.length() == 1)     //個
    {
        sd += GetCH(intInput);
        return sd;
    } else if (si.length() == 2)//十
    {
        if (si.substring(0, 1) == "1")
            sd += "十";
        else
            sd += (GetCH(intInput / 10) + "十");
        sd += ToCH(intInput % 10);
    } else if (si.length() == 3)//百
    {
        sd += (GetCH(intInput / 100) + "百");
        if (String.valueOf(intInput % 100).length() < 2 && (intInput % 100) != 0)
            sd += "零";
        sd += ToCH(intInput % 100);
    } else if (si.length() == 4)//千
    {
        sd += (GetCH(intInput / 1000) + "千");
        if (String.valueOf(intInput % 1000).length() < 3 && (intInput % 1000) != 0)
            sd += "零";
        sd += ToCH(intInput % 1000);
    } else if (si.length() == 5)//萬
    {
        sd += (GetCH(intInput / 10000) + "万");
        if (String.valueOf(intInput % 10000).length() < 4 && (intInput % 10000) != 0)
            sd += "零";
        sd += ToCH(intInput % 10000);
    }

    return sd;
}

/**
 * 字符串去重
 *
 * @param str
 * @return
 */
public static String removeRepeat(String str) {
    try {
        StringBuffer sb = new StringBuffer(str);
        String rs = sb.reverse().toString().replaceAll("(.)(?=.*\\1)", "");
        StringBuffer out = new StringBuffer(rs);
        return out.reverse().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return str;
    }
}

/**
 * list 去重
 *
 * @param list
 * @return
 */
public static List removeDuplicateList(List list) {
    HashSet h = new HashSet(list);
    list.clear();
    list.addAll(h);
    return list;
}

/**
 * 递归判断一个数是2的多少次方
 *
 * @param value
 * @return
 */
public static int log2(int value) {
    if (1 == value || 0 == value) {
        return 0;
    } else {
        return 1 + log2(value >> 1);
    }
}
/**
 * 获取2的幂次方
 *
 * @param n
 * @return
 */
public static int calculate(int n) {
    if (n == 0)
        return 1;
    return 2 * calculate(n - 1);
}

/**
 * 该函数判断一个字符串是否包含标点符号(中文英文标点符号)。
 * 原理是原字符串做一次清洗,清洗掉所有标点符号。
 * 此时,如果原字符串包含标点符号,那么清洗后的长度和原字符串长度不同。返回true。
 * 如果原字符串未包含标点符号,则清洗后长度不变。返回false。
 *
 * @param s
 * @return
 */
public static boolean check(String s) {
    boolean b = false;

    String tmp = s;
    tmp = tmp.replaceAll("\\p{P}", "");
    if (s.length() != tmp.length()) {
        b = true;
    }

    return b;
}
/**
 * @param source 被复制的实体类对象
 * @param to     复制完后的实体类对象
 * @throws Exception
 */
public static void Copy(Object source, Object to) throws Exception {
    // 获取属性
    BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), java.lang.Object.class);
    PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();

    BeanInfo destBean = Introspector.getBeanInfo(to.getClass(), java.lang.Object.class);
    PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();

    try {
        for (int i = 0; i < sourceProperty.length; i++) {

            for (int j = 0; j < destProperty.length; j++) {

                if (sourceProperty[i].getName().equals(destProperty[j].getName())) {
                    destProperty[j].getWriteMethod().invoke(to, sourceProperty[i].getReadMethod().invoke(source));
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new Exception("属性复制失败:" + e.getMessage());
    }
}

/**
 * 下划线转驼峰式
 *
 * @param str
 * @return
 */
public static String lineToHump(String str) {
    str = str.toLowerCase();
    Matcher matcher = linePattern.matcher(str);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
    }
    matcher.appendTail(sb);
    return sb.toString();
}

/**
 * 驼峰式转下划线
 *
 * @param str
 * @return
 */
public static String humpToLine2(String str) {
    Matcher matcher = humpPattern.matcher(str);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
    }
    matcher.appendTail(sb);
    return sb.toString();
}

/**
 * 手机号码加密
 *
 * @param phoneNumber
 * @return
 */
public static String maskPhoneNumber(String phoneNumber) {
    if (phoneNumber == null || phoneNumber.length() != 11 || !phoneNumber.matches("[0-9]+")) {
        return phoneNumber; 
    }
    return phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7);
}

/**
 * map 取key值自动转格式
 * @param map
 * @param key
 * @param c
 * @return
 * @param <T>
 */
public static <T> T paramValue(Map map, String key, Class c) {
    try {
        String v = null;
        if (map.containsKey(key)) {
            v = map.get(key).toString();
        } else {
            return null;
        }
        if ("".equalsIgnoreCase(v)) {
            return null;
        }
        if (Integer.class == c) {
            return (T) (Integer.valueOf(v));
        }
        if (String.class == c) {
            return (T) (v);
        }
        if (Long.class == c) {
            return (T) (Long.valueOf(v));
        }
        if (Double.class == c) {
            return (T) (Double.valueOf(v));
        }
        if (Boolean.class == c) {
            return (T) (Boolean.valueOf(v));
        }
        if (Date.class == c) {
            if (v.contains("-") && v.contains(":")) {
                //2024-08-05 16:49:17 时间格式
                return (T) (sdf.parse(v));
            }
            if (v.contains("/") && v.contains(":")) {
                //2024/08/05 16:49:17 时间格式
                return (T) (sdf4.parse(v));
            }
            if (v.length() == 13) {
                //时间戳 时间格式
                return (T) (new Date(Long.valueOf(v)));
            }
            if (v.contains("-")) {
                //时间戳 时间格式
                return (T) (format.parse(v));
            }
            if (v.length() == 8) {
                //时间戳 时间格式
                return (T) (sdf3.parse(v));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值