当前位置:网站首页>【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
}
}
边栏推荐
- day8
- CUDA相关
- Armeabi-v7a architecture (sv7a)
- UE4 common printing information methods for debugging
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
- andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
- Techo hub Fuzhou Station dry goods attack | talk with developers about new industrial intelligence technology
- Router view cannot be rendered (a very low-level error)
- Calculate properties and listeners
- 云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
猜你喜欢

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

Api 接口优化的那些技巧

Outlier detection and Gan network (1)

NPM run serve stuck at 40%

MySQL sub database and sub table and its smooth expansion scheme

I don't recommend you use Select*
![[Yugong series] go teaching course in July 2022, an array of 020 go containers](/img/06/b2f69599b30c4a93a6240613cbee84.png)
[Yugong series] go teaching course in July 2022, an array of 020 go containers

“吃货联盟定餐系统”

我不建议你使用SELECT *

Anomaly detection and unsupervised learning (2)
随机推荐
1331. Array sequence number conversion: simple simulation question
day8
There is a span tag. If you want to do click events on it, how can you expand the click area
SAP VL02N 交货单过账函数 WS_DELIVERY_UPDATE
Still writing a lot of if to judge? A rule executor kills all if judgments in the project
直流无刷电机控制器(换电机霍尔收费多少)
我不建议你使用SELECT *
Solutions such as failed plug-in installation and slow speed of linking remote server under vscode
靠云业务独撑收入增长大梁,微软仍然被高估?
Data warehouse construction - ads floor
Alibaba code index technology practice: provide reading experience of local IDE for code review
Calculate properties and listeners
Brief introduction to compressed sensing
Nftscan and nftplay have reached strategic cooperation in the field of NFT data
B站“崩溃”始末 2021.07.13 我们是这样崩的(转载)
SQL server only has database files and no log files. The solution to the 1813 error in restoring data times
flask与七牛云上传图片
Teach you how to install latex (nanny level tutorial)
Android必备的面试技能(含面试题和学习资料)
SQL Server 只有数据库文件,没有日志文件,恢复数据时报1813错误的解决方案