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:
parent
736a996837
commit
42b59131a0
@ -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>
|
||||||
|
@ -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>
|
||||||
|
Loading…
Reference in New Issue
Block a user