Java Random String getString(int length)

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

Description

get String

License

Apache License

Declaration

public static String getString(int length) 

Method Source Code


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

import java.util.Random;

public class Main {
    private static char[] digits = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
            'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'

    };/*from www .  ja  va  2 s . c  om*/
    private static int digitsSize = digits.length;

    public static String getString(int length) {

        char[] base = new char[length];

        Random random = new Random(System.nanoTime());

        for (int i = 0; i < base.length; i++) {
            base[i] = digits[random.nextInt(digitsSize)];

        }

        return new String(base);
    }
}

Related

  1. getRandomString(Random rnd, int minLength, int maxLength)
  2. getRandomString(String paramString, int paramLength)
  3. getRandomStringByLength(int length)
  4. getRandomStringForDate()
  5. getRandomStringOfLetters(int length)
  6. getString(int length)
  7. getString(int length)
  8. getString(int n, int arg[])
  9. randomCharacter(String availableValues)