Java Random String getRandomStringByLength(int length)

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

Description

get Random String By Length

License

Apache License

Declaration

public static String getRandomStringByLength(int length) 

Method Source Code

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

import java.util.*;

public class Main {

    public static String getRandomStringByLength(int length) {
        if (length == 0) {
            length = 32;/*  w  ww.ja v a 2 s. c  o  m*/
        }
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}

Related

  1. getRandomString(int strLength)
  2. getRandomString(Random random, int minLen, int maxLen)
  3. getRandomString(Random random, int totalLength, int randomSectionLength)
  4. getRandomString(Random rnd, int minLength, int maxLength)
  5. getRandomString(String paramString, int paramLength)
  6. getRandomStringForDate()
  7. getRandomStringOfLetters(int length)
  8. getString(int length)
  9. getString(int length)