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