RandomId 무 작위 문자열 생 성 실례


/*
 * :RandomId
 * : ID
 * :
 * :2012 1 31 03:36:28
 * :1.0
 * :2012 1 31 03:36:28
 */

using System;
using System.Text;

namespace blqw
{
    public sealed class RandomId : IFormattable
    {

        /// <summary> 1234567890qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM
        /// </summary>
        public const string ALLWORDS = "1234567890qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
        /// <summary> 2345678wertyuiopasdfghjkzxcvbnm23456780QWERTYUPASDFGHJKLZXCVBNM
        /// </summary>
        public const string SIMPLEWORDS = "2345678wertyuipasdfghjkzxcvbnm2345678WERTYUPASDFGHJKLZXCVBNM";

        #region
        const string ONE = "{0}";
        static RandomId _Rid = new RandomId(0);
        readonly static Random _Rand = new Random();
        static string ToFormat(int length)
        {
            StringBuilder sb = new StringBuilder(length * 3);
            for (int i = 0; i < length; i++)
            {
                sb.Append(ONE);
            }
            return sb.ToString();
        }

        readonly string _Dict;
        readonly int _RMax;
        readonly string _Format;
        #endregion

        /// <summary>
        /// </summary>
        /// <param name="length"> Id </param>
        /// <param name="dict"> , 0-9a-zA-Z</param>
        public RandomId(int length, string dict = ALLWORDS)
            : this(RandomId.ToFormat(length), dict)
        { }
        /// <summary>
        /// </summary>
        /// <param name="format"> Id </param>
        /// <param name="dict"> , 0-9a-zA-Z</param>
        public RandomId(string format, string dict = ALLWORDS)
        {
            _Dict = dict;
            _Format = format;
            _RMax = dict.Length;
        }

        /// <summary> Id
        /// </summary>
        public string Create()
        {
            return string.Format(_Format, this);
        }
        /// <summary> Id
        /// </summary>
        /// <param name="length"> Id </param>
        /// <param name="dict"> , 0-9a-zA-Z</param>
        public static string Create(int length, string dict = ALLWORDS)
        {
            return new RandomId(length, dict).Create();
        }
        /// <summary> Id
        /// </summary>
        /// <param name="format"> Id </param>
        /// <param name="dict"> , 0-9a-zA-Z</param>
        public static string Create(string format, string dict = ALLWORDS)
        {
            return new RandomId(format, dict).Create();
        }

        #region IFormattable

        string IFormattable.ToString(string format, IFormatProvider formatProvider)
        {
            return _Dict[_Rand.Next(0, _RMax)].ToString();
        }

        #endregion

       
    }
}


Console.WriteLine(RandomId.Create(4));// 4 , l,1,O,0,q,9
Console.WriteLine(RandomId.Create(4, RandomId.ALLWORDS));// (0-9a-zA-Z), 4
Console.WriteLine(RandomId.Create(10, " "));// , 4
Console.WriteLine(RandomId.Create("SN:{0}{0}{0}{0}-{0}{0}{0}-{0}{0}{0}.{0}{0}", "123456abcdef"));//

좋은 웹페이지 즐겨찾기