Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I need one help. I didn't understand how to auto generate no using character?
Normally i know autogenerate code only no e.g 1,2,3 n all but now i want to add AB1,AB2,AB3 n all. So what can i do?? Thanks in advance.
Posted

You can Use
public string CreateRandomStringWithNumber(int length)
    {
        const string validCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder _result = new StringBuilder();
        Random rnd = new Random();
        while (0 < length--)
        {
            _result.Append(validCharacters[rnd.Next(validCharacters.Length)]);
        }
        return _result.ToString();
    }

You can also give the character list from which you want to generate string.
 
Share this answer
 
I prefer LINQ :
C#
public static string GetRandomAlphaNumericString(int length)
  {
    const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var random = new Random();
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
  }


-KR
 
Share this answer
 
Comments
Member 11221185 3-Nov-15 5:13am    
thanks all
Got it
public void GetAutoNumberWithCharater(int length)
{
string Constring = string.Empty;
Constring = "AB";
for (int i = 1; i <= length; i++)
{
string getContString = Constring + i;

Console.WriteLine(getContString);
}
Console.ReadLine();
}
 
Share this answer
 
You can also use Guid.NewGuid() built in method
C#
public string CreateRandomStringWithNumber(int length)
    {
        string res = string.Empty;
        Random rn = new Random();
        int num = rn.Next(100, 999);
        string str = Convert.ToString(Guid.NewGuid()).Substring(0,7);
        res = str + num;
        return res;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900