随机生成任意长度字符串的方法(randomString)

首先,在你要用的类中创建一个randomString方法。代码如下:

private static String randomString(int length) {
        String xx = "";
        for (short i = '0'; i <= '9'; i++) {
            xx += (char) i;
        }
        for (short i = 'a'; i <= 'z'; i++) {
            xx += (char) i;
        }
        for (short i = 'A'; i <= 'Z'; i++) {
            xx += (char) i;
        }
        char yy[] = new char[length];
        for (int i = 0; i < yy.length; i++) {
            int index = (int) (Math.random() * xx.length());
            yy[i] = xx.charAt(index);
        }
        String result = new String(yy);
        return result;
    }

如果你想生成长度为10的字符串,实现如下:

String shengcheng = randomString(10);
        System.out.println(shengcheng);

就生成了长度为10的随机字符串  shengcheng 。

上一篇:cf873-Forbidden-Indices-后缀数组-并查集


下一篇:elasticsearch+kibana setup