■ 임의 문자열 구하기 예제

------------------------------------------------------------------------------------------------------------------------

string result = GetRandomString(10, RandomType.DigitAndLowerCharacter);

------------------------------------------------------------------------------------------------------------------------

 

■ 임의 문자열 구하기

------------------------------------------------------------------------------------------------------------------------

using System;

using System.Text;

 

/// <summary>

/// 임의 타입

/// </summary>

public static class RandomType

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Field

    ////////////////////////////////////////////////////////////////////////////////////////// Static

    //////////////////////////////////////////////////////////////////////////////// Public

 

    #region Field

 

    /// <summary>

    /// 숫자만

    /// </summary>

    public static readonly string OnlyDigit = "9";

 

    /// <summary>

    /// 소문자만

    /// </summary>

    public static readonly string OnlyLowerCharacter = "z";

 

    /// <summary>

    /// 대문자만

    /// </summary>

    public static readonly string OnlyUpperCharacter = "Z";

 

    /// <summary>

    /// 숫자와 대문자

    /// </summary>

    public static readonly string DigitANdUpperCharacter = "9Z";

 

    /// <summary>

    /// 숫자와 소문자

    /// </summary>

    public static readonly string DigitAndLowerCharacter = "9z";

 

    /// <summary>

    /// 모든 문자

    /// </summary>

    public static readonly string AllCharacter = "9Zz";

 

    #endregion

}

 

#region 임의 문자열 구하기 - GetRandomString(length, randomType)

 

/// <summary>

/// 임의 문자열 구하기

/// </summary>

/// <param name="length">길이</param>

/// <param name="randomType">임의 타입</param>

/// <returns>임의 문자열</returns>

public string GetRandomString(int length, string randomType = "9")

{

    StringBuilder result = new StringBuilder();

 

    int digitStart          = 48;

    int digitEnd            = 57;

    int upperCharacterStart = 65;

    int upperCharacterEnd   = 90;

    int lowerCharacterStart = 97;

    int lowerCharacterEnd   = 122;

            

    int start = 0;

    int end   = 0;

 

    Random random = new Random(DateTime.Now.Millisecond);

 

    for(int i = 1; i <= length; i++)

    {

        string nextRandomType = randomType;

 

        if(randomType.Length >=2)

        {

            Random nextRandom = new Random(DateTime.Now.Millisecond);

 

            int next = 1;

 

            switch(randomType.Length)

            {

                case 2 :

 

                    next = random.Next(1, 3);

 

                    if(next == 2)

                    {

                        if(randomType == "9Z")

                        {

                            next = 3;

                        }

                        else if(randomType == "9z")

                        {

                            next = 2;

                        }

                    }

 

                    break;

 

                case 3 :

 

                    next = random.Next(1, 4);

 

                    break;

            }

 

            switch(next)

            {

                case 1 : nextRandomType = "9"; break;

                case 2 : nextRandomType = "z"; break;

                case 3 : nextRandomType = "Z"; break;

            }

        }

 

        switch(nextRandomType)

        {

            case "Z" : start = upperCharacterStart; end = upperCharacterEnd; break;

            case "z" : start = lowerCharacterStart; end = lowerCharacterEnd; break;

            case "9" : start = digitStart;          end = digitEnd;          break;

        }

 

        result.Append(char.ConvertFromUtf32(random.Next(start, end + 1)));

    }

 

    return result.ToString();

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요