当前位置:网站首页>【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
}
}
边栏推荐
- Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
- Common measurement matrix and matlab code of compressed sensing
- Tips for API interface optimization
- Yield Guild Games:这一年的总结与未来展望
- Copy the table in word to wechat as a picture and send it
- 追踪伦敦银实时行情的方法
- DRF - deserialization of serializer, fields and parameters, local and global hooks, use of modelserializer
- 将行内元素转换为块元素的方法
- Matlab02: structured programming and function definition "suggestions collection"
- JWT token related configuration (global configuration identity authentication rewrites authenticate method)
猜你喜欢

将Word中的表格以图片形式复制到微信发送

Brief introduction to compressed sensing

Android必备的面试技能(含面试题和学习资料)

Statistical analysis of time series

Jupyter notebook中5个有趣的魔法命令

从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析

Data warehouse construction - ads floor

Requestvideoframecallback() simple instance

DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use

AQS principle
随机推荐
Api 接口优化的那些技巧
day8
第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄
Anomaly detection and unsupervised learning (1)
Have you seen the management area decoupling architecture? Can help customers solve big problems
DRF - deserialization of serializer, fields and parameters, local and global hooks, use of modelserializer
将行内元素转换为块元素的方法
day8
【无标题】
Alibaba code index technology practice: provide reading experience of local IDE for code review
IMG tags prohibit dragging pictures
CUDA相关
会议OA项目之会议通知&会议反馈&反馈详情功能
Summary of preprocessing methods for time series data
There is a span tag. If you want to do click events on it, how can you expand the click area
Copy the table in word to wechat as a picture and send it
NPM run serve stuck at 40%
Application and principle of distributed current limiting redistribution rratelimiter
Yield Guild Games:这一年的总结与未来展望
芯驰科技发布G9系列最新旗舰产品,配备6个1.8Ghz主频的A55核心