上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html

1.RandomStringUtils

1.1模拟实现random(count,str);

 1     //模拟实现random(5,"helloworld")
2 public static String getRandomString(int count,String str) {
3 if(str != null) {
4 if(!StringUtils.isBlank(str)) {
5 if(count <= 0) {
6 return "";
7 }
8 char[] charArray = str.toCharArray();
9 int index;
10 char[] newArray = new char[count];
11 for(int i=0;i<count;i++) {
12 index = RandomUtils.nextInt(0,charArray.length);
13 newArray[i] = str.charAt(index);
14 }
15 return new String(newArray);
16 }
17 }
18 return null;
19 }

1.2模拟实现randomAlphanumeric(字母与数字混合,可能没有数字)

 1 //模拟实现randomAlphanumeric(字母与数字混合)
2 public static String getRandomAlphanumeric(int count) {
3 if(count <=0) {
4 return "";
5 }
6 String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
7 int number = RandomUtils.nextInt(0,100000);
8 String str = letters + number;
9 return getRandomString(count, str);
10 }

1.3模拟实现可以指定数字个数的randomAlphanumeric

 1     //指定数字的个数
2 public static String getRandomAlphanumeric(int count,int numbers) {
3 if(count <=0 || numbers <=0) {
4 return "";
5 }
6 //纯数字
7 if(numbers>=count) {
8 return RandomStringUtils.randomNumeric(numbers);
9 }
10 String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
11 String str = RandomStringUtils.random(count-numbers, letters) + RandomStringUtils.randomNumeric(numbers);
12 //打乱位置
13 char[] charArray = str.toCharArray();
14 List<Character> list = new ArrayList<Character>();
15 for (Character character : charArray) {
16 list.add(character);
17 }
18 Collections.shuffle(list);
19 for(int i=0;i<list.size();i++) {
20 charArray[i] = list.get(i);
21 }
22 return new String(charArray);
23 }
24

总结:RandomStringUtils中的方法可以用于生成随机的验证字符串

2.RandomUtils

这个类感觉与java.util包下的Random类差别不大,还是那几个类似的方法(注意左闭右开)

相比较来说还是RandomStringUtils用处更多一点

RandomStringUtils RandomUtils的更多相关文章

  1. commons -lang(2) RandomStringUtils RandomUtils

    上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html 1.RandomStringUtils 1.1模拟实现random ...

  2. commons - lang(1) StringUtils

    分享几个关于StrngUtils的几个实用的方法(以下方法中省略了参数) 1.isBlank() 这个方法用来判空,包括null和空字符串,之前自己写的时候都是str != null &&am ...

  3. 20181108 Apache Commons Lang

    工具类 org.apache.commons.lang3 AnnotationUtils ArchUtils ArrayUtils BooleanUtils CharSetUtils CharUtil ...

  4. NumberUtils、ArrayUtils和RandomUtils工具类用法

    一.NumberUtils工具类 /*1.NumberUtils.isNumber():判断字符串是否是数字*/ NumberUtils.isNumber("5.96");//结果 ...

  5. Java 数字数组随机数工具类 NumberUtils、ArrayUtils、RandomUtils用法

    commons-lang3-3-3.8.1 //----------------------------------------------------------------------- /** ...

  6. RandomStringUtils

    System.out.println(RandomStringUtils.random(5));//随机多少个随机字符中文环境乱码 System.out.println(RandomStringUti ...

  7. RandomStringUtils的使用

    //产生5位长度的随机字符串,中文环境下是乱码 RandomStringUtils.random(5); //使用指定的字符生成5位长度的随机字符串 RandomStringUtils.random( ...

  8. RandomStringUtils工具类

    //产生5位长度的随机字符串,中文环境下是乱码 RandomStringUtils.random(5); //使用指定的字符生成5位长度的随机字符串 RandomStringUtils.random( ...

  9. RandomStringUtils工具类(java随机生成字符串)

    使用RandomStringUtils可以选择生成随机字符串,可以是全字母,全数字,自定义生成字符等等... 其最基础的方法: 参数解读: count:需要生成的随机串位数 letters:只要字母 ...

随机推荐

  1. 如何在同一台机器上安装多个MySQL的实例(转)

    最近由于工作的需要,需要在同一台机器上搭建两个MySQL的实例,(注:已经存在了一个3306的MySQL的实例). 先说下,什么是mysql的多实例,简单的来说就是一台机器上安装了多个mysql的服务 ...

  2. Microsoft SQL Server Version List(SQL Server 版本)

    原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...

  3. 初识ThreadLocal

    近期公司在进行Java开发者的招聘活动,当中有一道面试题是这种:"请简单描写叙述一下ThreadLocal类的作用." 结果发现有非常多的面试者没有听说过ThreadLocal或者 ...

  4. Android内存泄露分析之StrictMode

    转载请注明地址:http://blog.csdn.NET/yincheng886337/article/details/50524709 StrictMode(严格模式)使用 StrictMode严格 ...

  5. Android Service com.android.exchange.ExchangeService has leaked ServiceConnection

    启动Android项目的时候,clean  Project的时候,报错: android.app.ServiceConnectionLeaked: Service com.android.exchan ...

  6. 关于ES6(ES2015)的知识点详细总结

    ECMAScript6 ECMAScript简称就是ES,你可以把它看成是一套标准,JavaScript就是实施了这套标准的一门语言,现在主流浏览器使用的是ECMAScript5. http://ba ...

  7. 洛谷——P1046 陶陶摘苹果

    https://www.luogu.org/problem/show?pid=1046 题目描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出10个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有 ...

  8. oracle exp 备份脚本

    #!/bin/bash#Oracle 环境变量 NLS_LANG=AMERICAN_AMERICA.AL32UTF8 ORACLE_SID=zgw ORACLE_BASE=/opt/oracle OR ...

  9. 11.3 Android显示系统框架_最简单的surface测试程序

    APP有一个surface(界面),其有多个buffer用来存放界面数据,这些buffer是向surfaceflinger申请的: 因此我们编写的surface测试程序步骤: (1)获得surface ...

  10. php课程 2-7 php中常量如何定义

    php课程 2-7 php中常量如何定义 一.总结 一句话总结:函数方式定义和普通变量方式定义    define('PI','3.14').键值对,和session一样.    const PI=3 ...