Exemple #1
0
        /// <summary>
        /// 返回一个固定长度随机字符串。
        /// </summary>
        /// <param name="length">随机字符串的长度。</param>
        /// <param name="type">随机字符串的类型。</param>
        /// <returns>固定长度的随机字符串。</returns>
        public string NextString(int length, CharacterType type = CharacterType.Default)
        {
            if (length < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }
            char[]      chars      = new char[length];
            List <char> charsArray = new List <char>(40);

            if (type.HasFlag(CharacterType.UpperCase))
            {
                charsArray.AddRange(UpperCaseCharacters);
            }
            if (type.HasFlag(CharacterType.LowerCase))
            {
                charsArray.AddRange(LowerCaseCharacters);
            }
            if (type.HasFlag(CharacterType.Numeric))
            {
                charsArray.AddRange(NumericCharacters);
            }
            if (type.HasFlag(CharacterType.Special))
            {
                charsArray.AddRange(SpecialCharacters);
            }

            for (int i = 0; i < length; i++)
            {
                chars[i] = charsArray[this.Next(0, charsArray.Count)];
            }
            return(new string(chars));
        }
        private IEnumerable <Range <byte> > GetCharacterRanges(CharacterType characterType)
        {
            IEnumerable <Range <byte> > characterRangeList = Array.Empty <Range <byte> >();

            if (characterType.HasFlag(CharacterType.Lowercase))
            {
                characterRangeList = characterRangeList.Append(_rangeSwitch.Case(CharacterType.Lowercase));
            }

            if (characterType.HasFlag(CharacterType.Uppercase))
            {
                characterRangeList = characterRangeList.Append(_rangeSwitch.Case(CharacterType.Uppercase));
            }

            if (characterType.HasFlag(CharacterType.Numerics))
            {
                characterRangeList = characterRangeList.Append(_rangeSwitch.Case(CharacterType.Numerics));
            }

            if (characterType.HasFlag(CharacterType.Symbols))
            {
                characterRangeList = characterRangeList.Append(_rangeSwitch.Case(CharacterType.Symbols));
            }

            return(characterRangeList);
        }
Exemple #3
0
        /// <summary>
        /// 返回一个固定长度随机字符串。
        /// </summary>
        /// <param name="length">随机字符串的长度。</param>
        /// <param name="type">随机字符串的类型。</param>
        /// <returns>一个固定长度的随机字符串。</returns>
        public string NextString(int length, CharacterType type = CharacterType.Default)
        {
            if(length < 1) throw new ArgumentOutOfRangeException(nameof(length));
            char[] chars = new char[length];
            List<char> charsArray = new List<char>(40);
            if(type.HasFlag(CharacterType.UpperCase)) charsArray.AddRange(UpperCaseCharacters);
            if(type.HasFlag(CharacterType.LowerCase)) charsArray.AddRange(LowerCaseCharacters);
            if(type.HasFlag(CharacterType.Numeric)) charsArray.AddRange(NumericCharacters);
            if(type.HasFlag(CharacterType.Special)) charsArray.AddRange(SpecialCharacters);

            for(int i = 0; i < length; i++)
            {
                chars[i] = charsArray[this.Next(0, charsArray.Count)];
            }
            return new string(chars);
        }