Java Random String getString(int length)

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

Description

get String

License

Open Source License

Declaration

public static String getString(int length) 

Method Source Code


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

import java.util.Random;

public class Main {
    private static final char[] LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private static Random r = new Random();

    public static String getString(int length) {
        StringBuilder sb = new StringBuilder(length);
        while (length-- > 0) {
            sb.append(randomChar(LETTERS));
        }//from w w w.  j  av a2s. c o  m
        return sb.toString();
    }

    private static char randomChar(char[] letters) {
        int index = r.nextInt(letters.length);
        return letters[index];
    }
}

Related

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