Last active
August 14, 2018 22:01
-
-
Save abitofhelp/0424a04e95692c7e37e63fdc5cc97f8d to your computer and use it in GitHub Desktop.
This gist implements methods that extend the abilities of string objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
// file: Extensions\StringExtensions.cs | |
// | |
// summary: Implements the string extensions class | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
using System; | |
using System.Linq; | |
using System.Security.Cryptography; | |
namespace TVLib.Extensions | |
{ | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// <summary> String extensions. </summary> | |
/// | |
/// <remarks> Mike Gardner 12/05/16. </remarks> | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
public static class StringExtensions | |
{ | |
#region ENUMERATIONS | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// <summary> Values that represent random string contents. </summary> | |
/// | |
/// <remarks> Mike Gardner 12/05/16. </remarks> | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
public enum RandomStringContent : long | |
{ | |
/// <summary> An enum constant representing the alpha option. </summary> | |
LettersOnly = 0, | |
/// <summary> An enum constant representing the numeric option. </summary> | |
DigitsOnly = 1, | |
/// <summary> An enum constant representing the alphanumeric option. </summary> | |
AlphaNumeric = 2 | |
} | |
#endregion | |
#region CONSTANTS | |
/// <summary> The alpha numeric characters. </summary> | |
private const string kAlphaNumericCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
/// <summary> The digit characters. </summary> | |
private const string kDigitCharacters = "0123456789"; | |
/// <summary> The letter characters. </summary> | |
private const string kLetterCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
#endregion | |
#region ALL OTHER MEMBERS | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// <summary> | |
/// Remove all escape sequences from a string and replace them with a specified character. By | |
/// default, the character is nothing. | |
/// </summary> | |
/// | |
/// <remarks> Mike Gardner 12/09/16. </remarks> | |
/// | |
/// <param name="text"> Text to sanitize. </param> | |
/// <param name="replacement"> (Optional) Character used to replace each escape sequence. </param> | |
/// | |
/// <returns> Null on error. </returns> | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
public static string RemoveEscapeSequences(this string text, string replacement = "") | |
{ | |
if (string.IsNullOrEmpty(text)) | |
{ | |
return null; | |
} | |
var cleaned = text.Replace("\a", replacement) // Warning | |
.Replace("\b", replacement) // BACKSPACE | |
.Replace("\f", replacement) // Form-feed | |
.Replace("\n", replacement) // Line reverse | |
.Replace("\r", replacement) // Carriage return | |
.Replace("\t", replacement) // Horizontal tab | |
.Replace("\v", replacement) // Vertical tab | |
.Replace("\'", replacement) // Single quote | |
.Replace("\"", replacement) // Double quote | |
.Replace("\\", replacement); // Backslash | |
//.Replace ("\?", replacement); // Literal question mark | |
return cleaned; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// <summary> A string extension method that converts a text "boolean" to a boolean. </summary> | |
/// | |
/// <remarks> Mike Gardner 12/12/16. </remarks> | |
/// | |
/// <param name="text"> Text to sanitize. </param> | |
/// | |
/// <returns> A boolean value or null on error. </returns> | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
public static bool? ToBoolean(this string text) | |
{ | |
if (text.Equals("Yes") || text.Equals("True") || text.Equals("Y") || text.Equals("T")) return true; | |
if (text.Equals("No") || text.Equals("False") || text.Equals("N") || text.Equals("F")) return false; | |
return null; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// <summary> Generates a random string of a desired length. </summary> | |
/// | |
/// <remarks> | |
/// Mike Gardner 12/05/16. The random string is not guaranteed to be unique. | |
/// </remarks> | |
/// | |
/// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside | |
/// the required range. </exception> | |
/// | |
/// <param name="content"> The content. </param> | |
/// <param name="length"> (Optional) The length. </param> | |
/// | |
/// <returns> The random string, or null on error. </returns> | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
public static string GenerateRandomString(RandomStringContent content, uint length = 10) | |
{ | |
var permittedCharacters = string.Empty; | |
switch (content) | |
{ | |
case RandomStringContent.LettersOnly: | |
permittedCharacters = kLetterCharacters; | |
break; | |
case RandomStringContent.DigitsOnly: | |
permittedCharacters = kDigitCharacters; | |
break; | |
case RandomStringContent.AlphaNumeric: | |
permittedCharacters = kAlphaNumericCharacters; | |
break; | |
default: | |
throw new ArgumentOutOfRangeException(nameof(content), content, null); | |
} | |
return new string( | |
Enumerable | |
// Permitted characters and length | |
.Repeat(permittedCharacters, (int) length) | |
.Select( | |
s => | |
{ | |
var cryptoResult = new byte[4]; | |
new RNGCryptoServiceProvider() | |
.GetBytes(cryptoResult); | |
return | |
s[ | |
new Random( | |
BitConverter | |
.ToInt32( | |
cryptoResult, | |
0)) | |
.Next(s.Length)]; | |
}).ToArray()); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment