Java Random String getRandomString(int length)

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

Description

get Random String

License

Apache License

Declaration

public static String getRandomString(int length) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.List;
import java.util.Random;

public class Main {

    public static String getRandomString(int length) {
        if (length <= 0) {
            return "";
        }/*from  w  w  w  .j ava  2 s .  co m*/
        char[] randomChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'q', 'w', 'e', 'r', 't', 'y', 'u',
                'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            stringBuffer.append(randomChar[Math.abs(random.nextInt()) % randomChar.length]);
        }
        return stringBuffer.toString();
    }

    public static String toString(List<String> list, String separator) {
        StringBuffer stringBuffer = new StringBuffer();
        for (String str : list) {
            stringBuffer.append(separator + str);
        }
        stringBuffer.deleteCharAt(0);
        return stringBuffer.toString();
    }
}

Related

  1. getRandomString(int len)
  2. getRandomString(int len)
  3. getRandomString(int len)
  4. getRandomString(int len, boolean ascii_only)
  5. getRandomString(int lenght)
  6. getRandomString(int length)
  7. getRandomString(int length)
  8. getRandomString(int length)
  9. getRandomString(int length)