Class StringExtension
Extension methods and utilities for string manipulation, random string generation, and Base64 encoding.
public static class StringExtension
- Inheritance
-
StringExtension
- Inherited Members
Fields
AlphaNumericCharacters
public const string AlphaNumericCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Field Value
BinaryCharacters
public const string BinaryCharacters = "01"
Field Value
ExtendedCharacters
public const string ExtendedCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%()*+,-.:;<=>?@[]^_`{|}~"
Field Value
HexCharacters
public const string HexCharacters = "0123456789ABCDEF"
Field Value
LowerCaseAlphaNumericCharacters
public const string LowerCaseAlphaNumericCharacters = "abcdefghijklmnopqrstuvwxyz0123456789"
Field Value
LowerCaseCharacters
public const string LowerCaseCharacters = "abcdefghijklmnopqrstuvwxyz"
Field Value
NumericCharacters
public const string NumericCharacters = "0123456789"
Field Value
OctCharacters
public const string OctCharacters = "01234567"
Field Value
UpperCaseAlphaNumericCharacters
public const string UpperCaseAlphaNumericCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Field Value
UpperCaseCharacters
public const string UpperCaseCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Field Value
UriSafeCharacters
public const string UriSafeCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
Field Value
Methods
FromBase64(string)
Decodes a Base64 string to its UTF-8 representation. Returns null if the input is null.
public static string FromBase64(this string item)
Parameters
itemstring
Returns
GetRandomString(int, int, string)
Generates a cryptographically random string with length between minLength and maxLength.
public static string GetRandomString(int minLength = 8, int maxLength = 16, string availableCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
Parameters
minLengthintMinimum length of the generated string.
maxLengthintMaximum length of the generated string.
availableCharactersstringThe character set to draw from.
Returns
Exceptions
- ArgumentException
Thrown when the character set is null or empty.
- ArgumentOutOfRangeException
Thrown when the length range is invalid.
IfEmpty(string, string)
Returns a fallback value if the string is null or empty.
public static string IfEmpty(this string item, string value)
Parameters
Returns
IsNullOrEmpty(string)
Returns true if the string is null or empty.
public static bool IsNullOrEmpty(this string item)
Parameters
itemstring
Returns
NullIfEmpty(string)
Returns null if the string is empty; otherwise returns the original string.
public static string NullIfEmpty(this string item)
Parameters
itemstring
Returns
Random(string, int, int)
Returns a random string from provided array.
public static string Random(this string characters, int minLength = 8, int maxLength = 16)
Parameters
Returns
RandomString(int, int, string)
[Obsolete("Use GetRandomString instead.")]
public static string RandomString(int minLength = 8, int maxLength = 16, string availableCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
Parameters
Returns
RandomString(int, string)
Returns a random string of alphanumeric characters given a specific length.
public static string RandomString(this int length, string availableCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
Parameters
Returns
ToBase64(string)
Encodes the string to Base64 using UTF-8. Returns null if the input is null.
public static string ToBase64(this string item)
Parameters
itemstring
Returns
Truncate(string, int)
Truncates the string to the specified maximum length.
public static string Truncate(this string item, int maxLength)