2013年9月22日星期日

[Android] String problems seek help

String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 @. + - * / _";

If the string above , I would like on the inside eight randomly selected to form a new string , how to achieve ?

passing under the guidance of the great God .
------ Solution ---------------------------------------- ----
here did not consider repeated
	public void testString()
{
    Random random = new Random();
    String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@.+-*/_";
    
    int str_len = str.length();
    StringBuffer stringBuffer = new StringBuffer();
    for(int i=0; i<8; i++)
    {
        int temp_radom =  random.nextInt(str_len-1)%(str_len);
        stringBuffer.append(str.substring(temp_radom, temp_radom+1));
    }
    System.out.println(stringBuffer.toString());
}

没有评论:

发表评论