java生成随机字符串的两种方法
作者:ATJAVA 时间:2022-09-09 22:49:26
本文实例为大家分享了java生成随机字符串的具体代码,供大家参考,具体内容如下
import java.util.Random;
public class CharacterUtils {
//方法1:length为产生的位数
public static String getRandomString(int length){
//定义一个字符串(A-Z,a-z,0-9)即62位;
String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
//由Random生成随机数
Random random=new Random();
StringBuffer sb=new StringBuffer();
//长度为几就循环几次
for(int i=0; i<length; ++i){
//产生0-61的数字
int number=random.nextInt(62);
//将产生的数字通过length次承载到sb中
sb.append(str.charAt(number));
}
//将承载的字符转换成字符串
return sb.toString();
}
/**
* 第二种方法
*/
public static String getRandomString2(int length){
//产生随机数
Random random=new Random();
StringBuffer sb=new StringBuffer();
//循环length次
for(int i=0; i<length; i++){
//产生0-2个随机数,既与a-z,A-Z,0-9三种可能
int number=random.nextInt(3);
long result=0;
switch(number){
//如果number产生的是数字0;
case 0:
//产生A-Z的ASCII码
result=Math.round(Math.random()*25+65);
//将ASCII码转换成字符
sb.append(String.valueOf((char)result));
break;
case 1:
//产生a-z的ASCII码
result=Math.round(Math.random()*25+97);
sb.append(String.valueOf((char)result));
break;
case 2:
//产生0-9的数字
sb.append(String.valueOf
(new Random().nextInt(10)));
break;
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(CharacterUtils.getRandomString(12));
}
}
来源:http://www.cnblogs.com/ipetergo/p/7636982.html
标签:java,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Spring Cloud Stream使用延迟消息实现定时任务(RabbitMQ)
2022-09-14 05:07:34
![](https://img.aspxhome.com/file/2023/5/72905_0s.png)
springcloud初体验(真香)
2022-12-08 06:33:44
![](https://img.aspxhome.com/file/2023/8/68728_0s.jpg)
mybatis初始化SqlSessionFactory失败的几个原因分析
2021-06-17 11:13:44
![](https://img.aspxhome.com/file/2023/3/62083_0s.png)
详解java Collections.sort的两种用法
2023-11-28 09:30:31
![](https://img.aspxhome.com/file/2023/3/60473_0s.png)
c# 读取XML文件的示例
2023-11-04 00:51:17
![](https://img.aspxhome.com/file/2023/4/77284_0s.png)
Spring boot集成Kafka消息中间件代码实例
2022-11-06 21:53:48
![](https://img.aspxhome.com/file/2023/6/61786_0s.jpg)
Spring的初始化和XML解析的实现
2022-12-13 15:50:51
Unity3D运行报DllNotFoundException错误的解决方案
2021-09-13 00:22:35
![](https://img.aspxhome.com/file/2023/0/78150_0s.png)
Servlet3.0实现文件上传的方法
2023-08-15 00:52:44
![](https://img.aspxhome.com/file/2023/3/58143_0s.png)
Spring Cloud项目前后端分离跨域的操作
2022-05-20 08:11:16
java8 stream中Collectors.toMap空指针问题及解决
2023-01-16 13:05:28
![](https://img.aspxhome.com/file/2023/2/63662_0s.png)
基于java构造方法Vector查找元素源码分析
2023-11-29 04:33:30
![](https://img.aspxhome.com/file/2023/6/60236_0s.png)
Java中byte输出write到文件的实现方法讲解
2023-12-25 11:11:36
基于ThreadLocal 的用法及内存泄露(内存溢出)
2023-11-23 20:22:28
![](https://img.aspxhome.com/file/2023/9/66509_0s.png)
Java开发中synchronized的定义及用法详解
2021-11-11 07:01:20
![](https://img.aspxhome.com/file/2023/6/62226_0s.png)
C#8.0新语法using declaration
2023-10-23 00:57:54
Java 归并排序算法、堆排序算法实例详解
2023-11-25 09:43:25
![](https://img.aspxhome.com/file/2023/6/59896_0s.jpg)
聊聊Java 中的线程中断
2021-05-31 02:04:30
Spring Boot MQTT Too many publishes in progress错误的解决方案
2023-06-27 02:16:44
![](https://img.aspxhome.com/file/2023/4/75754_0s.webp)
C# 添加对System.Configuration.dll文件的引用操作
2022-03-05 22:20:31
![](https://img.aspxhome.com/file/2023/7/69807_0s.jpg)