첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ 예제

string result = RandomStringHelper.GetRandomString(10, RandomStringHelper.AllCharacter);

 

▶ RandomStringHelper.cs

using System;
using System.Text;

/// <summary>
/// 임의 문자열 헬퍼
/// </summary>
public static class RandomStringHelper
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// 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

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Public

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

    /// <summary>
    /// 임의 문자열 구하기
    /// </summary>
    /// <param name="length">길이</param>
    /// <param name="randomType">임의 타입</param>
    /// <returns>임의 문자열</returns>
    public static 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 _ = 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
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요