Java随机取字符串的工具类
来源:互联网 发布:阿里云攻击最长封多久 编辑:程序博客网 时间:2024/05/14 03:13
原文:Java随机取字符串的工具类
源代码下载地址:http://www.zuidaima.com/share/1550463479532544.htm
Java 随机取字符串的工具类 可以全部是数字,字符,也可以字符和数字组合的工具类,希望能给大家带来帮助
package com.zuidaima.test;import java.util.Random;public class RandomUtils {public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";public static final String NUMBERCHAR = "0123456789"; /** * 返回一个定长的随机字符串(只包含大小写字母、数字) * @param length 随机字符串长度 * @author www.zuidaima.com * @return 随机字符串 */ public static String generateString(int length){StringBuffer sb = new StringBuffer();Random random = new Random();for (int i = 0; i < length; i++) {sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));}return sb.toString();}/** * 返回一个定长的随机纯字母字符串(只包含大小写字母) * @param length 随机字符串长度 * @author www.zuidaima.com * @return 随机字符串 */ public static String generateMixString(int length) { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0; i < length; i++) { sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length()))); } return sb.toString(); } /** * 返回一个定长的随机纯大写字母字符串(只包含大小写字母) * * @param length 随机字符串长度 * @return 随机字符串 */ public static String generateLowerString(int length) { return generateMixString(length).toLowerCase(); } /** * 返回一个定长的随机纯小写字母字符串(只包含大小写字母) * * @param length 随机字符串长度 * @author www.zuidaima.com * @return 随机字符串 */ public static String generateUpperString(int length) { return generateMixString(length).toUpperCase(); } /** * 生成一个定长的纯0字符串 * * @param length 字符串长度 * @return 纯0字符串 */ public static String generateZeroString(int length) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { sb.append('0'); } return sb.toString(); } /** * 根据数字生成一个定长的字符串,长度不够前面补0 * * @param num 数字 * @param fixdlenth 字符串长度 * @author www.zuidaima.com * @return 定长的字符串 */ public static String toFixdLengthString(long num, int fixdlenth) { StringBuffer sb = new StringBuffer(); String strNum = String.valueOf(num); if (fixdlenth - strNum.length() >= 0) { sb.append(generateZeroString(fixdlenth - strNum.length())); } else { throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常!"); } sb.append(strNum); return sb.toString(); } /** * 根据数字生成一个定长的字符串,长度不够前面补0 * * @param num 数字 * @param fixdlenth 字符串长度 * @author www.zuidaima.com * @return 定长的字符串 */ public static String toFixdLengthString(int num, int fixdlenth) { StringBuffer sb = new StringBuffer(); String strNum = String.valueOf(num); if (fixdlenth - strNum.length() >= 0) { sb.append(generateZeroString(fixdlenth - strNum.length())); } else { throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常!"); } sb.append(strNum); return sb.toString(); } /** * 每次生成的len位数都不相同 * @param param * @return 定长的数字 */ public static int getNotSimple(int[] param,int len) { Random rand = new Random(); for (int i = param.length; i > 1; i--) { int index = rand.nextInt(i); int tmp = param[index]; param[index] = param[i - 1]; param[i - 1] = tmp; } int result = 0; for(int i = 0; i < len; i++) { result = result * 10 + param[i]; } return result; } public static void main(String[] args) { System.out.println(generateString(10)); System.out.println(generateMixString(10)); System.out.println(generateLowerString(10)); System.out.println(generateUpperString(10)); System.out.println(generateZeroString(10)); System.out.println(toFixdLengthString(123, 10)); System.out.println(toFixdLengthString(123L, 10)); int[] in = {1,2,3,4,5,6,7}; System.out.println(getNotSimple(in,3)); } }
0 0
- Java随机取字符串的工具类
- 生成随机字符串的工具类
- JAVA---字符串中随机取四位数
- java产生随机字符串的各种方法------工具类(包含过滤表情方法)
- Java工具类_随机生成任意长度的字符串【密码、验证码】
- 工具类-获取一定长度的随机字符串
- Java的JSON取值工具类
- JAVA工具类(10)--- 随机生成字符串工具类randomUtil
- JAVA工具类(14)----UUID工具类,产生随机字符串主键
- java list 取随机
- 微信支付--工具类--随机字符串
- Java获取随机数生成随机字符串用于验证码等之类的工具 (未测试)
- java生成随机短信验证码的工具类
- 取得随机字符串的类
- c#工具类,用于产生随机整数,随机字符串,随机颜色等
- JAVA中随机字符串的取得
- Java生成随机字符串的代码
- java生成指定长度的随机字符串
- Java核心技术卷I:基础知识(原书第8版):12.5 泛型代码和虚拟机
- Navicat for MySQL数据库连接
- Installation. Linux下 Eclipse Mars 安装 c++ 插件,并运行 helloworld
- 指针
- Android常用UI组件 - Button
- Java随机取字符串的工具类
- 阿里校招附加题2014-8-29
- RAC结构删除一个节点并删除数据库软件的解决方案
- Lua 学习笔记(八) ——实例(一)
- 内存到底分几个区(转)
- Java核心技术卷I:基础知识(原书第8版):12.6 约束与局限性
- STL源码中map和set中key值不能修改的实现
- Eclipse导入工程出错The type java.lang.String cannot be resolved. It is indirectly referenced from required
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法