Can anyone tell me how can I generate a random string containing only letters in c#? I basically want a Random value and fill it in my form. I want this value to contain letters only? How can I do this?
-
stackoverflow.com/questions/3066707/…David Brabant– David Brabant2017-09-24 13:00:37 +00:00Commented Sep 24, 2017 at 13:00
-
or using LINQ https://stackoverflow.com/questions/1344221/how-can-i-generate-random-alphanumeric-strings-in-cstyx– styx2017-09-24 13:02:30 +00:00Commented Sep 24, 2017 at 13:02
Add a comment
|
1 Answer
You could use the Random class to generate random numbers between 1 and 26, and convert that to characters. Something like this:
Random rnd = new Random();
int length = 20;
var str = "";
for(var i = 0; i < length; i++)
{
str += ((char)(rnd.Next(1,26) + 64)).ToString();
}
Console.WriteLine(str);
1 Comment
Orel Eraki
There are already plenty of example of previous questions and answers + the OP didn't shown any attempt he tried.