Java Password Generate generateString(final String letters, int length)

Here you can find the source of generateString(final String letters, int length)

Description

Generates quasi-random String of given length.

License

Open Source License

Parameter

Parameter Description
letters string containing symbols for new string
length length of the result string

Return

generated string

Declaration

public static String generateString(final String letters, int length) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    public static final Random r = new Random();

    /**//from  w w  w  .j  a  v  a  2  s  . co  m
     * Generates quasi-random String of given length.
     * To generate passwords use SecurityHelper class.
     *
     * @param letters   string containing symbols for new string
     * @param length    length of the result string
     * @return          generated string
     */
    public static String generateString(final String letters, int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(letters.charAt(r.nextInt(letters.length())));
        }
        return sb.toString();
    }

    public static String generateString(int length, final String... letters) {
        StringBuilder sb = new StringBuilder();
        while (length-- > 0) {
            sb.append(letters[r.nextInt(letters.length)]);
        }
        return sb.toString();
    }
}

Related

  1. generatePassword(int length)
  2. generatePassword(int length)
  3. generatePassword(int length, boolean special)
  4. generatePassword(int length, String combination)
  5. generatePassword(int minLen, int maxLen, int noOfCAPSAlpha, int noOfDigits, int noOfSplChars)
  6. generateString(int length)
  7. generateString(int length)
  8. generateString(int length)
  9. generateString(int length)