zhanghua
2025-04-14 1cad14bca191807e18705c3a5526eda8151be439
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.ycl.common.util;
 
import java.util.Random;
 
public class RandomStringUtil {
    public static final String NUMBERS = "0123456789";
    public static final String LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
    public static final String CAPITAL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String CAPITAL_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String CAPITAL_NUM_LOWER = "0123456789abcdefghijklmnopqrstuvwxyz";
    public static final String ALL = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
 
    
    /**
     * 生成指定长度的随机字符串
     * 
     * @param length
     * @return
     */
    public static String getRandomString(int length, String base) { // length表示生成字符串的长度
        int baseLength = base.length();
        Random random = new Random();
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(baseLength);
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(getRandomString(6, RandomStringUtil.NUMBERS));
    }
}