C # generate random strings,

Source: Internet
Author: User

C # generate random strings,

1. The code is reprinted. Record it here.

/// <Summary> /// generate a random string /// </summary> /// <param name = "length"> length of the target string </param> // /<param name = "useNum"> whether it contains numbers, 1 = include. The default value is include </param> // <param name = "useLow"> whether to include lowercase letters, 1 = include, the default value is include </param> /// <param name = "useUpp"> whether to include uppercase letters, 1 = include, the default value is include </param> /// <param name = "useSpe"> whether it contains special characters, 1 = include, the default value is </param> /// <param name = "custom">, enter the list of characters to be included </param> // <returns> A random string of the specified length </returns> public static string GetRan DomString (int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom) {byte [] B = new byte [4]; new System. security. cryptography. RNGCryptoServiceProvider (). getBytes (B); Random r = new Random (BitConverter. toInt32 (B, 0); string s = null, str = custom; if (useNum = true) {str + = "0123456789" ;}if (useLow = true) {str + = "abcdefghijklmnopqrstuvwxyz";} if (useUpp = true) {st R + = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";} if (useSpe = true) {str + = "! \ "# $ % & '() * +,-./:; <=>? @ [\] ^ _ '{| }~ ";}For (int I = 0; I <length; I ++) {s + = str. substring (r. next (0, str. length-1), 1);} return s ;}

 

Related Article

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.