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 ...
随机推荐
- 关于myeclipse8.6的优化设置
1.设置一些类型文件的编码格式: >preferences>General>Content Types>Text>javaScript sourcefile 相同的操作 ...
- Tomcat下载,新建自己的项目,模拟server
一.tomcat下载 下载地址http://tomcat.apache.org/ 打开网页能够看到例如以下内容 在网页左边有Download以下就是能够下载的版本号.如6.0,7.0,8.0: 选择一 ...
- Apache HTTPserver安装后报:无法启动,由于应用程序的并行配置不对-(已解决)
原创作品.出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处.否则有权追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...
- this的指向问题
在 ES5 中,其实 this 的指向,始终坚持一个原理:this 永远指向最后调用它的那个对象. 例 1: var name = "windowsName"; function ...
- img和父容器之间有间隙的问题
在前端开发中,经常遇到在一个img外面套div的时候,div的大小和img的大小并不一样,在底部会有一段空白. 代码如下: <div> <img src = ''imgs/1.jpg ...
- sublime text 3配置使用python
1. 在sublime text的官网下载,是适合自己系统的版本.官网地址:https://www.sublimetext.com/3 2. 安装好后,在菜单栏打开:Preferences---> ...
- springMVC上传错误StandardMultipartHttpServletRequest
异常信息 java.lang.ClassCastException: org.springframework.security.web.servletapi.HttpServlet3RequestFa ...
- 【java】抓取页面内容,提取链接(此方法可以http get无需账号密码的请求)
package 网络编程; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileOutpu ...
- php trim源码分析
本文同时发表于https://github.com/zhangyachen/zhangyachen.github.io/issues/9 核心代码如下: /* {{{ php_trim() * mod ...
- mysql创建新用户及新用户不能本地登陆的问题
最近在搭建hadoop集群,主节点上面安装的MySQL数据库,对着方面不熟悉,为hive.Ooize等服务统一使用的root账号和密码,为了安全一些库对于某些用户是不可见的,所以需要针对不同的服务设置 ...