Java Password Generate generateString(int length)

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

Description

Creates a random string using only lowercase ascii letters, uppercase ascii letters, and digits.

License

Apache License

Parameter

Parameter Description
length The length of random string to generate

Return

A new random string with a length specified in the length parameter

Declaration

public static String generateString(int length) 

Method Source Code

//package com.java2s;
/*//from  w  w w. j  a va2  s .  c  o  m
 * #%L
 * ch-commons-util
 * %%
 * Copyright (C) 2012 Cloudhopper by Twitter
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.Random;

public class Main {
    private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static Random random = new Random();

    /**
     * Creates a random string using only lowercase ascii letters, uppercase
     * ascii letters, and digits.  This method internally uses a static instance
     * of a Random() object.
     * @param length The length of random string to generate
     * @return A new random string with a length specified in the length parameter
     */
    public static String generateString(int length) {
        char[] buf = new char[length];
        for (int i = 0; i < buf.length; i++) {
            buf[i] = chars.charAt(random.nextInt(chars.length()));
        }
        return new String(buf);
    }
}

Related

  1. generatePassword(int length, String combination)
  2. generatePassword(int minLen, int maxLen, int noOfCAPSAlpha, int noOfDigits, int noOfSplChars)
  3. generateString(final String letters, int length)
  4. generateString(int length)
  5. generateString(int length)
  6. generateString(int length)
  7. GenerateString(int Length)
  8. generateString(int maxlength)
  9. generateString(String alphabet, int length)