Java Random String getRandomString(int length)

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

Description

get Random String

License

LGPL

Declaration

public static String getRandomString(int length) 

Method Source Code

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

import java.util.Random;

public class Main {

    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();

        if (length < 1) {
            length = 1;/*from   ww  w .j a v  a  2 s .  c  o  m*/
        }
        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static int length(String str) {
        if (str == null)
            return 0;
        char[] c = str.toCharArray();
        int len = 0;
        for (int i = 0; i < c.length; i++) {
            len++;
            if (!isLetter(c[i])) {
                len++;
            }
        }
        return len;
    }

    private static boolean isLetter(char charStr) {
        int k = 0x80;
        return charStr / k == 0 ? true : false;
    }
}

Related

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