From 42b59131a040a55f29303a157437fdc1bb215b3a Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 18 Dec 2019 13:05:44 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Char[].Random=20and=20String.?= =?UTF-8?q?Random?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- X10D/src/CharExtensions.cs | 56 ++++++++++++++++++++++++++++++++++++ X10D/src/StringExtensions.cs | 23 +++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/X10D/src/CharExtensions.cs b/X10D/src/CharExtensions.cs index 328f53f..fe76e58 100644 --- a/X10D/src/CharExtensions.cs +++ b/X10D/src/CharExtensions.cs @@ -3,6 +3,10 @@ #region Using Directives using System; + using System.Collections; + using System.Collections.Generic; + using System.Linq; + using System.Text; #endregion @@ -11,6 +15,58 @@ /// public static class CharExtensions { + /// + /// Generates a new random string by filling it with characters found in . + /// + /// The character set. + /// The length of the string to generate. + /// Returns a containing characters. + public static string Random(this char[] chars, int length) + { + return chars.Random(length, new Random()); + } + + /// + /// Generates a new random string by filling it with characters found in . + /// + /// The character set. + /// The length of the string to generate. + /// The instance. + /// Returns a containing characters. + public static string Random(this char[] chars, int length, Random random) + { + StringBuilder builder = new StringBuilder(length); + for (int i = 0; i < length; i++) + { + builder.Append(chars.ElementAt(random.Next(0, chars.Length))); + } + + return builder.ToString(); + } + + /// + /// Generates a new random string by filling it with characters found in . + /// + /// The character set. + /// The length of the string to generate. + /// Returns a containing characters. + public static string Random(this IEnumerable chars, int length) + { + return chars.Random(length, new Random()); + } + + /// + /// Generates a new random string by filling it with characters found in . + /// + /// The character set. + /// The length of the string to generate. + /// The instance. + /// Returns a containing characters. + public static string Random(this IEnumerable chars, int length, Random random) + { + return chars.ToArray().Random(length, random); + } + /// /// Repeats a character a specified number of times. /// diff --git a/X10D/src/StringExtensions.cs b/X10D/src/StringExtensions.cs index ea2e50e..71a9025 100644 --- a/X10D/src/StringExtensions.cs +++ b/X10D/src/StringExtensions.cs @@ -108,6 +108,29 @@ return encoding.GetBytes(str); } + /// + /// Generates a new random string by filling it with characters found in . + /// + /// The character set. + /// The length of the string to generate. + /// Returns a containing characters. + public static string Random(this string str, int length) + { + return str.Random(length, new Random()); + } + + /// + /// Generates a new random string by filling it with characters found in . + /// + /// The character set. + /// The length of the string to generate. + /// The instance. + /// Returns a containing characters. + public static string Random(this string str, int length, Random random) + { + return str.ToCharArray().Random(length, random); + } + /// /// Repeats a string a specified number of times. ///