1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 03:45:41 +00:00

Add Char[].Random and String.Random

This commit is contained in:
Oliver Booth 2019-12-18 13:05:44 +00:00
parent 736a996837
commit 42b59131a0
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
2 changed files with 79 additions and 0 deletions

View File

@ -3,6 +3,10 @@
#region Using Directives #region Using Directives
using System; using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion #endregion
@ -11,6 +15,58 @@
/// </summary> /// </summary>
public static class CharExtensions public static class CharExtensions
{ {
/// <summary>
/// Generates a new random string by filling it with characters found in <see cref="chars"/>.
/// </summary>
/// <param name="chars">The character set.</param>
/// <param name="length">The length of the string to generate.</param>
/// <returns>Returns a <see cref="String"/> containing <paramref name="length"/> characters.</returns>
public static string Random(this char[] chars, int length)
{
return chars.Random(length, new Random());
}
/// <summary>
/// Generates a new random string by filling it with characters found in <see cref="chars"/>.
/// </summary>
/// <param name="chars">The character set.</param>
/// <param name="length">The length of the string to generate.</param>
/// <param name="random">The <see cref="System.Random"/> instance.</param>
/// <returns>Returns a <see cref="String"/> containing <paramref name="length"/> characters.</returns>
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();
}
/// <summary>
/// Generates a new random string by filling it with characters found in <see cref="chars"/>.
/// </summary>
/// <param name="chars">The character set.</param>
/// <param name="length">The length of the string to generate.</param>
/// <returns>Returns a <see cref="String"/> containing <paramref name="length"/> characters.</returns>
public static string Random(this IEnumerable<char> chars, int length)
{
return chars.Random(length, new Random());
}
/// <summary>
/// Generates a new random string by filling it with characters found in <see cref="chars"/>.
/// </summary>
/// <param name="chars">The character set.</param>
/// <param name="length">The length of the string to generate.</param>
/// <param name="random">The <see cref="System.Random"/> instance.</param>
/// <returns>Returns a <see cref="String"/> containing <paramref name="length"/> characters.</returns>
public static string Random(this IEnumerable<char> chars, int length, Random random)
{
return chars.ToArray().Random(length, random);
}
/// <summary> /// <summary>
/// Repeats a character a specified number of times. /// Repeats a character a specified number of times.
/// </summary> /// </summary>

View File

@ -108,6 +108,29 @@
return encoding.GetBytes(str); return encoding.GetBytes(str);
} }
/// <summary>
/// Generates a new random string by filling it with characters found in <see cref="str"/>.
/// </summary>
/// <param name="str">The character set.</param>
/// <param name="length">The length of the string to generate.</param>
/// <returns>Returns a <see cref="String"/> containing <paramref name="length"/> characters.</returns>
public static string Random(this string str, int length)
{
return str.Random(length, new Random());
}
/// <summary>
/// Generates a new random string by filling it with characters found in <see cref="str"/>.
/// </summary>
/// <param name="str">The character set.</param>
/// <param name="length">The length of the string to generate.</param>
/// <param name="random">The <see cref="System.Random"/> instance.</param>
/// <returns>Returns a <see cref="String"/> containing <paramref name="length"/> characters.</returns>
public static string Random(this string str, int length, Random random)
{
return str.ToCharArray().Random(length, random);
}
/// <summary> /// <summary>
/// Repeats a string a specified number of times. /// Repeats a string a specified number of times.
/// </summary> /// </summary>