Here you can find the source of getRandomString(int length)
public static String getRandomString(int length)
//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; } }