commons -lang(2) RandomStringUtils RandomUtils
上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html
1.RandomStringUtils

1.1模拟实现random(count,str);
//模拟实现random(5,"helloworld")
public static String getRandomString(int count,String str) {
if(str != null) {
if(!StringUtils.isBlank(str)) {
if(count <= 0) {
return "";
}
char[] charArray = str.toCharArray();
int index;
char[] newArray = new char[count];
for(int i=0;i<count;i++) {
index = RandomUtils.nextInt(0,charArray.length);
newArray[i] = str.charAt(index);
}
return new String(newArray);
}
}
return null;
}
1.2模拟实现randomAlphanumeric(字母与数字混合,可能没有数字)
//模拟实现randomAlphanumeric(字母与数字混合)
public static String getRandomAlphanumeric(int count) {
if(count <=0) {
return "";
}
String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int number = RandomUtils.nextInt(0,100000);
String str = letters + number;
return getRandomString(count, str);
}
1.3模拟实现可以指定数字个数的randomAlphanumeric
//指定数字的个数
public static String getRandomAlphanumeric(int count,int numbers) {
if(count <=0 || numbers <=0) {
return "";
}
//纯数字
if(numbers>=count) {
return RandomStringUtils.randomNumeric(numbers);
}
String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String str = RandomStringUtils.random(count-numbers, letters) + RandomStringUtils.randomNumeric(numbers);
//打乱位置
char[] charArray = str.toCharArray();
List<Character> list = new ArrayList<Character>();
for (Character character : charArray) {
list.add(character);
}
Collections.shuffle(list);
for(int i=0;i<list.size();i++) {
charArray[i] = list.get(i);
}
return new String(charArray);
}
总结:RandomStringUtils中的方法可以用于生成随机的验证字符串
2.RandomUtils
这个类感觉与java.util包下的Random类差别不大,还是那几个类似的方法(注意左闭右开)

相比较来说还是RandomStringUtils用处更多一点
commons -lang(2) RandomStringUtils RandomUtils的更多相关文章
- Apache Commons Lang
http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/package- ...
- 20181108 Apache Commons Lang
工具类 org.apache.commons.lang3 AnnotationUtils ArchUtils ArrayUtils BooleanUtils CharSetUtils CharUtil ...
- Java工具类之Apache的Commons Lang和BeanUtils
Apache Commons包估计是Java中使用最广发的工具包了,很多框架都依赖于这组工具包中的一部分,它提供了我们常用的一些编程需要,但是JDK没能提供的机能,最大化的减少重复代码的编写. htt ...
- apache commons lang架包介绍
commons lang组件介绍和学习 介绍 Java语言开发时有一个隐患,那就是java支持null值,这就导致很多时候操作可能会出异常. 因此很多第三方组件都会提供安全null safe 操作(即 ...
- Apache Commons Lang » 3.10使用简介
============================================================= 行文介绍: 1.诞生背景 2.引入方案 3.简单介绍 4 .详情介绍 文档: ...
- 让时间处理简单化 【第三方扩展类库org.apache.commons.lang.time】
JAVA的时间日期处理一直是一个比较复杂的问题,大多数程序员都不能很轻松的来处理这些问题.首先Java中关于时间的类,从 JDK 1.1 开始,Date的作用很有限,相应的功能已由Calendar与D ...
- 关于出现 org.apache.commons.lang.exception.NestableRuntimeException的解决方法
最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说 ...
- org.apache.commons.lang.StringUtils中常用的方法
org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...
- java转换json需要导入的jar包,org/apache/commons/lang/exception/NestableRuntimeException
缺少相应jar包都会有异常,根据异常找jar包导入...... 这里我说下lang包,因为这个包我找了好半天: 我用的是: commons-lang3-3.1.jar 出现异常: jav ...
随机推荐
- 掌上快递 APP 项目之概述篇
概述 学习Android开发也有一段时间了,利用业余时间独立制作的一款快递类APP软件.大概2个多星期吧,自己将其定位为"集快递信息追踪.附近快递点查询. 快递公司投诉功能为一体的便民生活类 ...
- 家居环境监測系统设计(PC上位机版)(手机APP版待定)
下面是我的毕业设计:家居环境监測系统设计(PC上位机临时版.手机app版待定).本系统採用STC12C5A60S2单片机.结合传感器.分别对空气湿度.空气温度.气压.海拔.进水温度.出水温度.光照强度 ...
- Android DVM
1.DVM(Dalvik Virtual Machine)概述 是Google公司自己设计用于Android平台的Java虚拟机 支持已经转化为.dex(及Dalvik Excutable)格式的Ja ...
- JavaSe:Comparator
今天,公司里有一个萌萌的妹子问我java 中的comparator是怎么回事.参数分别是什么,返回值又是什么,为此,我写了一个简单的程序告诉了她: public static void main (S ...
- 598. Range Addition II
Given an m * n matrixMinitialized with all0's and several update operations. Operations are represen ...
- 日志框架之Logback
1 日志框架选择 日志门面:SLF4J 日志实现:Logback 2 实现控制台的日志打印输出01 2.1 在需要实现日志信息打印的类中实例化Logger对象 private final Logger ...
- 阿里云ECS升级OpenSSL记录
1.下载OpenSSL wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz 2.解压编译安装 tar xf openssl-1.1.0e ...
- ES6 let和const命令(4)
const声明的常量只在当前代码块有效.如果想设置跨模块的常量,可以采用下面的写法. //constants.js模块 export const A = 1; export const B = 3; ...
- BeanShell断言(一)
在beanShell中直接可以调用的变量,无需加前缀. 1.log 打印日志 log.info(“在控制台打印日志”); 2.SampleResult 获取SampleResult对象,可以通过这个对 ...
- 基于MATLAB的中值滤波均值滤波以及高斯滤波的实现
基于MATLAB的中值滤波均值滤波以及高斯滤波的实现 作者:lee神 1. 背景知识 中值滤波法是一种非线性平滑技术,它将每一像素点的灰度值设置为该点某邻域窗口内的所有像素点灰度值的中值. 中值滤 ...