当前位置:网站首页>【commons-lang3专题】003- RandomStringUtils 专题
【commons-lang3专题】003- RandomStringUtils 专题
2022-07-28 23:29:00 【訾博ZiBo】
【commons-lang3专题】003- RandomStringUtils 专题
文章目录
〇、准备
1、RandomStringUtils 主要作用
提供生成各种随机字符串方法。
2、引入依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
一、随机字符串
1、生成指定长度的随机字符串(字母数字外的所有字符集)
// 1、生成指定长度的随机字符串(字母数字外的所有字符集)
// 从所有字符集中选择字符,不含字母和数字
System.out.println(RandomStringUtils.random(5)); // 𥘿𦊿途
2、生成指定长度的随机字符串(指定字符集)
// 2、生成指定长度的随机字符串(指定字符集)
// 语法:random(int count, boolean letters, boolean numbers)
// letters: 字母字符集 numbers:数字字符集
// 随机字符串由字母和数字组成
System.out.println(RandomStringUtils.random(5, true, true)); // ny6Oh
// 随机字符串由字母组成
System.out.println(RandomStringUtils.random(5, true, false)); // edrYz
// 随机字符串由数字组成
System.out.println(RandomStringUtils.random(5, false, true)); // edrYz
// 随机字符串由字母数字外的所有字符集组成(等同于random(int count))
System.out.println(RandomStringUtils.random(5, false, false)); // ᷉줅𝥛뚣
3、从指定字符集数组中生成指定长度的随机字符串
// 3、从指定字符集数组中生成指定长度的随机字符串
System.out.println(RandomStringUtils.random(5, 'A', 'B', '1', '2')); // 2B21B
4、从指定的字符集中生成指定长度的字符串
// 4、从指定的字符集中生成指定长度的字符串
System.out.println(RandomStringUtils.random(5, "bkjfsdhjkkj32h54386tweuyfb345")); // 3u4e4
二、随机字母字符串
5、生成指定长度的随机字母字符串
// 5、生成指定长度的随机字母字符串
// 相当于 random(count, true, false)
System.out.println(RandomStringUtils.randomAlphabetic(5)); // yaalN
6、生成介于两个长度之间的随机字母字符串
// 6、生成介于两个长度之间的随机字母字符串
System.out.println(RandomStringUtils.randomAlphabetic(5, 10)); // uuIpdckTS
三、随机数字字符串
7、生成指定长度的随机数字字符串
// 13、生成指定长度的随机数字字符串
System.out.println(RandomStringUtils.randomNumeric(5)); // 56789
8、生成介于两个长度之间的随机数字字符串
// 14、生成介于两个长度之间的随机数字字符串
System.out.println(RandomStringUtils.randomNumeric(8, 16)); // 81179252
四、随机字母或数字字符串
9、生成指定长度的随机字母或数字字符串
// 7、生成指定长度的随机字母或数字字符串
// 相当于 random(count, true, true)
System.out.println(RandomStringUtils.randomAlphanumeric(5)); // n91KB
10、生成介于两个长度之间的随机字母或数字字符串
// 8、生成介于两个长度之间的随机字母或数字字符串
System.out.println(RandomStringUtils.randomAlphanumeric(5, 10)); // vs5cw8Q
五、随机 ASCII 码字符串
11、生成指定长度的随机 ASCII 码字符串
// 9、生成指定长度的随机 ASCII 码字符串
// ASCII 码值介于 [32,126] 之间,相当于 random(count, 32, 127, false, false)
System.out.println(RandomStringUtils.randomAscii(5)); // 7*>/#
12、生成介于两个长度之间的随机 ASCII 码字符串
// 10、生成介于两个长度之间的随机 ASCII 码字符串
System.out.println(RandomStringUtils.randomAscii(8, 16)); // ;TD/+kHB2n$Dg
13、生成指定长度的可见的随机 ASCII 码字符串
// 11、生成指定长度的可见的随机 ASCII 码字符串
// 即除空格和控制字符外的任何内容,相当于 random(count, 33, 126, false, false)
System.out.println(RandomStringUtils.randomGraph(5)); // ")N$R
14、生成介于两个长度之间的可见的随机 ASCII 码字符串
// 12、生成介于两个长度之间的可见的随机 ASCII 码字符串
System.out.println(RandomStringUtils.randomGraph(8, 16)); // *?'u{iDDN=#{b&T
六、随机字符串-正则匹配集
15、生成指定长度的随机字符串(从正则表达式字符类匹配的字符集)
// 15、生成指定长度的随机字符串(从正则表达式字符类匹配的字符集)
// 从正则表达式字符类匹配的字符集中选择字符,不含空格和控制字符
System.out.println(RandomStringUtils.randomPrint(5)); // L*bh1
16、生成介于两个长度之间的随机字符串(从正则表达式字符类匹配的字符集)
// 16、生成介于两个长度之间的随机字符串(从正则表达式字符类匹配的字符集)
// 从正则表达式字符类匹配的字符集中选择字符,不含空格和控制字符
System.out.println(RandomStringUtils.randomPrint(8, 16)); // 4D-.4V\ZH
七、完整代码
package com.zibo.zibo2022.random_string_utils.main;
import org.apache.commons.lang3.RandomStringUtils;
public class Main {
public static void main(String[] args) {
// start
// 1、生成指定长度的随机字符串(字母数字外的所有字符集)
// 从所有字符集中选择字符,不含字母和数字
System.out.println(RandomStringUtils.random(5)); // 𥘿𦊿途
// 2、生成指定长度的随机字符串(指定字符集)
// 语法:random(int count, boolean letters, boolean numbers)
// letters: 字母字符集 numbers:数字字符集
// 随机字符串由字母和数字组成
System.out.println(RandomStringUtils.random(5, true, true)); // ny6Oh
// 随机字符串由字母组成
System.out.println(RandomStringUtils.random(5, true, false)); // edrYz
// 随机字符串由数字组成
System.out.println(RandomStringUtils.random(5, false, true)); // edrYz
// 随机字符串由字母数字外的所有字符集组成(等同于random(int count))
System.out.println(RandomStringUtils.random(5, false, false)); // ᷉줅𝥛뚣
// 3、从指定字符集数组中生成指定长度的随机字符串
System.out.println(RandomStringUtils.random(5, 'A', 'B', '1', '2')); // 2B21B
// 4、从指定的字符集中生成指定长度的字符串
System.out.println(RandomStringUtils.random(5, "bkjfsdhjkkj32h54386tweuyfb345")); // 3u4e4
// 5、生成指定长度的随机字母字符串
// 相当于 random(count, true, false)
System.out.println(RandomStringUtils.randomAlphabetic(5)); // yaalN
// 6、生成介于两个长度之间的随机字母字符串
System.out.println(RandomStringUtils.randomAlphabetic(5, 10)); // uuIpdckTS
// 7、生成指定长度的随机字母或数字字符串
// 相当于 random(count, true, true)
System.out.println(RandomStringUtils.randomAlphanumeric(5)); // n91KB
// 8、生成介于两个长度之间的随机字母或数字字符串
System.out.println(RandomStringUtils.randomAlphanumeric(5, 10)); // vs5cw8Q
// 9、生成指定长度的随机 ASCII 码字符串
// ASCII 码值介于 [32,126] 之间,相当于 random(count, 32, 127, false, false)
System.out.println(RandomStringUtils.randomAscii(5)); // 7*>/#
// 10、生成介于两个长度之间的随机 ASCII 码字符串
System.out.println(RandomStringUtils.randomAscii(8, 16)); // ;TD/+kHB2n$Dg
// 11、生成指定长度的可见的随机 ASCII 码字符串
// 即除空格和控制字符外的任何内容,相当于 random(count, 33, 126, false, false)
System.out.println(RandomStringUtils.randomGraph(5)); // ")N$R
// 12、生成介于两个长度之间的可见的随机 ASCII 码字符串
System.out.println(RandomStringUtils.randomGraph(8, 16)); // *?'u{iDDN=#{b&T
// 13、生成指定长度的随机数字字符串
System.out.println(RandomStringUtils.randomNumeric(5)); // 56789
// 14、生成介于两个长度之间的随机数字字符串
System.out.println(RandomStringUtils.randomNumeric(8, 16)); // 81179252
// 15、生成指定长度的随机字符串(从正则表达式字符类匹配的字符集)
// 从正则表达式字符类匹配的字符集中选择字符,不含空格和控制字符
System.out.println(RandomStringUtils.randomPrint(5)); // L*bh1
// 16、生成介于两个长度之间的随机字符串(从正则表达式字符类匹配的字符集)
// 从正则表达式字符类匹配的字符集中选择字符,不含空格和控制字符
System.out.println(RandomStringUtils.randomPrint(8, 16)); // 4D-.4V\ZH
// end
}
}
边栏推荐
- Error reporting: the network preview shows {xxx:['this field is required']}
- UE4 common printing information methods for debugging
- Andriod6.0 low power mode (turn off WiFi, Bluetooth, GPS, screen brightness, etc.)
- Soft test --- database (4) SQL statement
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
- Five interesting magic commands in jupyter notebook
- 会议OA项目之会议通知&会议反馈&反馈详情功能
- day8
- Depth first search (DFS) and its matlab code
- ZABBIX deployment and monitoring
猜你喜欢

Educational Codeforces Round 132 (Rated for Div. 2)【A~C】

I don't know how lucky the boy who randomly typed the log is. There must be a lot of overtime!

管理区解耦架构见过吗?能帮客户搞定大难题的

【无标题】

Outlier detection and open set identification (1)

数仓搭建——ADS层

Relying on cloud business to support revenue growth alone, is Microsoft still overvalued?

Api 接口优化的那些技巧

数仓搭建——DWT层

会议OA项目之会议通知&会议反馈&反馈详情功能
随机推荐
Requestvideoframecallback() simple instance
rk3399 9.0驱动添加Powser按键
【飞控开发基础教程8】疯壳·开源编队无人机-I2C(激光测距)
Calculate properties and listeners
数仓搭建——DWT层
Common measurement matrix and matlab code of compressed sensing
CUDA相关
Error reporting: when the browser clicks the modify add button, there is no response and no error reporting. Solution
Copy the table in word to wechat as a picture and send it
Anomaly detection and unsupervised learning (1)
DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
主线程与守护线程
Common sparse basis and matlab code for compressed sensing
selenium对接代理与seleniumwire访问开发者工具NetWork
Introduction of shortest path tree (SPT) and matlab code
Summary of preprocessing methods for time series data
Brief introduction to compressed sensing
Copu Professor Lu Shouqun was invited to give a keynote speech at the open atom global open source summit
Application and principle of distributed current limiting redistribution rratelimiter
ORACLE not available如何解决