Java Random String getRandomString(int len, boolean ascii_only)

Here you can find the source of getRandomString(int len, boolean ascii_only)

Description

get Random String

License

Open Source License

Declaration

private static String getRandomString(int len, boolean ascii_only) 

Method Source Code

//package com.java2s;

import java.util.Random;

public class Main {
    private static String getRandomString(int len, boolean ascii_only) {
        Random r = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            char c;
            if (ascii_only) {
                c = (char) r.nextInt(62);
                if (c < 10) {
                    c = (char) (48 + c);
                } else if (c < 36) {
                    c = (char) (65 + c - 10);
                } else {
                    c = (char) (97 + c - 36);
                }/*from w  w w . jav  a2s  .  c o m*/
            } else {
                c = (char) (r.nextInt(94) + 32);
            }
            // if (c == '{' || c == '}' || c == '`')
            // {
            // continue;
            // }
            sb.append(c);
        }
        return sb.toString();
    }
}

Related

  1. getRandomString(int count)
  2. getRandomString(int len)
  3. getRandomString(int len)
  4. getRandomString(int len)
  5. getRandomString(int len)
  6. getRandomString(int lenght)
  7. getRandomString(int length)
  8. getRandomString(int length)
  9. getRandomString(int length)