Tuesday, November 8, 2011

How to get the random string in asp.net

Here is the code in code-behind file.

private static string RandomString(int size, bool lowerCase) {

StringBuilder builder = new StringBuilder();

Random random = new Random();

char ch;

for(int i = 0;i < size;i++) {

ch = Convert.ToChar(Convert.ToInt32(26 * random.NextDouble() + 65));

builder.Append(ch);

}

if(lowerCase)

return builder.ToString().ToLower();

return builder.ToString();

}



You can pass the parameters such as length of string and lower case or not.

No comments:

Post a Comment