Java Random String getRandomString(int length)

Here you can find the source of getRandomString(int length)

Description

get Random String

License

Open Source License

Declaration

public static String getRandomString(int length) 

Method Source Code


//package com.java2s;
import java.util.Random;

public class Main {
    private static final String src_all = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String getRandomString(int length) {
        StringBuffer r = new StringBuffer(length);
        String src = src_all;/*from www. jav a2s  .c om*/
        for (int i = 0; i < length; i++) {
            r.append(getRandomStr(src));
        }
        return r.toString();
    }

    private static final String getRandomStr(String src) {
        if (null == src || "".equals(src)) {
            return "";
        }
        Random random = new Random();
        return String.valueOf(src.charAt(random.nextInt(src.length())));
    }
}

Related

  1. getRandomString(int length)
  2. getRandomString(int length)
  3. getRandomString(int length)
  4. getRandomString(int length)
  5. getRandomString(int length)
  6. getRandomString(int length)
  7. getRandomString(int length)
  8. getRandomString(int length)
  9. getRandomString(int length)