■ 임의 문자열을 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 |
string result = RandomStringHelper.GetRandomString(10, RandomStringHelper.AllCharacter); |
▶ RandomStringHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
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 } |