jiangping
2024-11-23 c9aa84299c67f0b17585ef58c9c586f76a8a4796
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.doumee.core.utils;
 
 
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.security.SecureRandom;
import java.util.Random;
 
/**
 * A null-safe utility to handle the String instances.
 *
 * @author Guang YANG
 * @version 1.1
 */
public final class Strings {
 
    public static final String EMPTY = "";
    public static final String INSTANTIATION_PROHIBITED = "Instantiation prohibited.";
    private static final Logger LOGGER = LoggerFactory.getLogger(Strings.class);
 
    private Strings() {
        throw new AssertionError(INSTANTIATION_PROHIBITED);
    }
 
    /**
     * Generate random string with alpha and numeric of given length.
     */
    public static String randomAlphanumeric(int length) {
        return new RandomString(length).nextString();
    }
 
    public static String randomNumeric(int length) {
        return new RandomString(length, new SecureRandom(), "0123456789").nextString();
    }
 
    public static String getRandomNumberString(Integer strLength) {
 
        Random rnd = new Random();
 
        String str = "9999";
 
        if(strLength >= str.length()){
            str = StringUtils.leftPad(str,strLength,"9");
        }
 
        int number = rnd.nextInt(Integer.parseInt(str));
 
        return String.format("%06d", number);
 
    }
 
 
}