JAVA生成隨機數工具類RandomStringUtils詳解


來自:https://blog.csdn.net/yaomingyang/article/details/79107764

=================================================================

public static String random(int count, boolean letters, boolean numbers)

  /**
         * count 創建一個隨機字符串,其長度是指定的字符數,字符將從參數的字母數字字符集中選擇,如參數所示。
         * letters true,生成的字符串可以包括字母字符
         * numbers true,生成的字符串可以包含數字字符
         */
        String random = RandomStringUtils.random(15, true, false);
        System.out.println(random);

 

public static String random(int count) --- 中文下會是亂碼,所有字符集

        /**
         * 創建一個隨機字符串,其長度是指定的字符數。
         * 將從所有字符集中選擇字符
         */
        random = RandomStringUtils.random(22);
        System.out.println(random);

 

public static String random(int count, String chars)

        /**
         * 創建一個隨機字符串,其長度是指定的字符數。
         * 字符將從字符串指定的字符集中選擇,不能為空。如果NULL,則使用所有字符集。
         */
        random = RandomStringUtils.random(15, "abcdefgABCDEFG123456789");
        System.out.println(random);這里寫代碼片

 

public static String random(int count, int start,int end,boolean letters, boolean numbers)

        /**
         * 創建一個隨機字符串,其長度是指定的字符數,字符將從參數的字母數字字符集中選擇,如參數所示。
         * count:計算創建的隨機字符長度
         * start:字符集在開始時的位置
         * end:字符集在結束前的位置,必須大於65
         * letters true,生成的字符串可以包括字母字符
         * numbers true,生成的字符串可以包含數字字符
         * 
         */
        random = RandomStringUtils.random(1009, 5, 129, true, true);這里寫代碼片

 

public static String randomAlphabetic(int count)

        /**
         * 產生一個長度為指定的隨機字符串的字符數,字符將從拉丁字母(a-z、A-Z的選擇)。
         * count:創建隨機字符串的長度
         */
        random = RandomStringUtils.randomAlphabetic(15);

 

public static String randomAlphabetic(int minLengthInclusive, int maxLengthExclusive)

        /**
         * 創建一個隨機字符串,其長度介於包含最小值和最大最大值之間,,字符將從拉丁字母(a-z、A-Z的選擇)。
         * minLengthInclusive :要生成的字符串的包含最小長度
         * maxLengthExclusive :要生成的字符串的包含最大長度
         */
        random = RandomStringUtils.randomAlphabetic(2, 15);這里寫代碼片

 

 

public static String randomAlphanumeric(int count) ==== 這個常用,已經經過測試沒有問題

        /**
         * 創建一個隨機字符串,其長度是指定的字符數,字符將從拉丁字母(a-z、A-Z)和數字0-9中選擇。
         * count :創建的隨機數長度
         */
        random = RandomStringUtils.randomAlphanumeric(15);這里寫代碼片

 

 

public static String randomAlphanumeric(int minLengthInclusive,int maxLengthExclusive

        /**
         * 創建一個隨機字符串,其長度介於包含最小值和最大最大值之間,字符將從拉丁字母(a-z、A-Z)和數字0-9中選擇。
         * minLengthInclusive :要生成的字符串的包含最小長度
         * maxLengthExclusive :要生成的字符串的包含最大長度
         * 
         */
        random = RandomStringUtils.randomAlphanumeric(5, 68);這里寫代碼片

 

public static String randomAscii(int count)

        /**
         * 創建一個隨機字符串,其長度是指定的字符數,字符將從ASCII值介於32到126之間的字符集中選擇(包括)
         * count:隨機字符串的長度
         */
        random = RandomStringUtils.randomAscii(15);

 

public static String randomAscii(int minLengthInclusive, int maxLengthExclusive)

        /**
         * 創建一個隨機字符串,其長度介於包含最小值和最大最大值之間,字符將從ASCII值介於32到126之間的字符集中選擇(包括)
         * minLengthInclusive :要生成的字符串的包含最小長度
         * maxLengthExclusive :要生成的字符串的包含最大長度
         */
        random = RandomStringUtils.randomAscii(15, 30);這里寫代碼片

 

public static String randomNumeric(int count) === 這個也是常用

        /**
         * 創建一個隨機字符串,其長度是指定的字符數,將從數字字符集中選擇字符。
         * count:生成隨機數的長度
         */
        random = RandomStringUtils.randomNumeric(15);

 

public static String randomNumeric(int minLengthInclusive, int maxLengthExclusive)

        /**
         * 創建一個隨機字符串,其長度介於包含最小值和最大最大值之間,將從數字字符集中選擇字符.
         * minLengthInclusive, 要生成的字符串的包含最小長度
         * maxLengthExclusive 要生成的字符串的包含最大長度
         */
        random = RandomStringUtils.randomNumeric(15, 20);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM