C # Custom Function netxtstring Generate random string

Source: Internet
Author: User

This example shows the C # Custom function Netxtstring Implementation of the method of generating random strings, in the development of C # project is very practical! Share to everyone for your reference.


First, generate a random string

The key code is as follows:

<summary>///generate random string///</summary>///<param name= "random" >random</param>///<param Name= "Size" > string length </param>///<param name= "lowercase" > string is lowercase </param>///<returns> random string </returns>public static string netxtstring (this random random, int size, bool lowercase) {  StringBuilder _ Builder = new StringBuilder (size);  int _startchar = lowercase? 97:65;//65 = A/97 = A for  (int i = 0; i < size; i++) _builder. Append ((char) (* random. Nextdouble () + _startchar));  Return _builder. ToString ();}

The test code is as follows:

static void Main (string[] args) {  try  {random _random = new Random (); for (int i = 0; i <; i++) {   Consol E.writeline (_random.netxtstring (4, false)); }  }  catch (Exception ex)  {Console.WriteLine (ex). Message);  }  Finally  {console.readline ();  }}

The test results are as follows:


Generate random strings based on specified characters

The key code is as follows:

///<summary>///0~9 a~z string///</summary>public static string Randomstring_ 09AZ = "0123456789ABCDEFGHIJKMLNOPQRSTUVWXYZ";///<summary>///generates a random string based on the specified string///</summary>///< param name= "random" >random</param>///<param name= "RandomString" > Specify string </param>///<param Name= "Size" > string length </param>///<param name= "lowercase" > string is lowercase </param>///<returns> random string </returns>public static string netxtstring (this random random, string randomstring, int size, bool lowercase) {Stri ng _nextstring = string.  Empty; if (random! = null &&!string.  IsNullOrEmpty (RandomString)) {StringBuilder _builder = new StringBuilder (size); int _maxcount = randomstring.length-1; for (int i = 0; i < size; i++) {int _number = random.   Next (0, _maxcount); _builder. Append (Randomstring[_number]); } _nextstring = _builder.  ToString (); } return lowercase? _nextstring.tolower (): _nextstring.toupper ();} 

The test code is as follows:

static void Main (string[] args) {  try  {random _random = new Random (); string _ramdomstring = Randomtoolv2.randomst Ring_09az; for (int i = 0; i <; i++) {   //Console.WriteLine (_random.netxtstring (4, false));   Console.WriteLine (_random.netxtstring (_ramdomstring, 4, false)); }  }  catch (Exception ex)  {Console.WriteLine (ex). Message);  }  Finally  {console.readline ();  }}

The code works as shown in the following:

I hope that the examples described in this article can help you in C # learning!

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Custom Function netxtstring Generate random string

This address: http://www.paobuke.com/develop/c-develop/pbk23645.html






Related content C # development Portal and Applications (2) Message handling and Response C # jigsaw Puzzle writing code (2) C # Basics Get started-variables use C # language to implement the query condition interface expansion and unwinding function
C # Online English-Chinese dictionary applet for Beginners C # WinForm shortcut setting tips C # Implementing methods for importing a CSV file into an Excel workbook in C # LINQ to XML

C # Custom Function netxtstring Generate random string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.