工具类:自动生成名字工具类

public class CharUtil {
    /**
     * 将字符串转换成相应的字符编码
     * @param s
     * @return
     */
    public static String bytes2HexString(String s) {
        byte[] b = s.getBytes();
        s = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            s = s + hex.toUpperCase();
        }
        return s;
    }

    /**
     * 随机生成汉字
     * @return
     */
    public static String getRandomChar() {
        String str = "";
        int highCode;
        int lowCode;

        Random random = new Random();

        highCode = (176 + Math.abs(random.nextInt(39))); //B0 + 0~39(16~55) 一级汉字所占区
        lowCode = (161 + Math.abs(random.nextInt(93))); //A1 + 0~93 每区有94个汉字

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(highCode)).byteValue();
        b[1] = (Integer.valueOf(lowCode)).byteValue();

        try {
            str = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 随机生成2-4个字的名字
     * @return
     */
    public static String getRandomName() {
        Random random = new Random();
        //名字的个数
        int charNum = random.nextInt(4) % 3 + 2;
        String resultName = "";
        for (int i= 0 ; i<charNum; i++) {
            String nameWord = getRandomChar();
            resultName += nameWord;
        }
        return resultName;
    }

直接调方法中getRandomName()方法就行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值