Home  >  Article  >  Java  >  Two ways to generate random strings in Java

Two ways to generate random strings in Java

黄舟
黄舟Original
2017-10-09 10:02:381658browse

This article mainly introduces two methods of generating random strings in Java in detail. It has certain reference value. Interested friends can refer to it.

The examples in this article share Java with you. The specific code to generate a random string is for your reference. The specific content is as follows


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

The above is the detailed content of Two ways to generate random strings in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn