String Extensions - adyle5/ExtensionsDotNet GitHub Wiki
Description
Maps to string.IsNullOrWhiteSpace
returns true if string is null or white space, false otherwise.
Parameters
None
Usage
string someString = "this is a string";
if (someString.IsNullOrWhiteSpaceExt()) {
// do something
}
Description
maps to string.IsNullOrEmpty
returns true if string is null or white space, false otherwise.
Parameters
None
Usage
string someString = "this is a string";
if (someString.IsNullOrEmptyExt())
{
// do something
}
Description
Returns false if string is null or white space, otherwise returns true.
Parameters
None
Usage
string someString = " ";
if (someString.IsNotNullOrWhiteSpaceExt()) {
// This is true!
}
Description
Returns false if string is null or empty, otherwise returns true.
Parameters
None
Usage
string someString = string.Empty;
if (someString.IsNotNullOrEmptyExt())
{
// This is true!
}
****Description
maps to Maps to string.compareTo
In addition, sets the String comparison to Invariant Culture Ignore Case.
returns 0 if the values are the same (ignoring case) and -1 if they are different.
Parameters
compareTo (string) - The string to compare
Usage
string str1 = "Apples";
string str2 = "apples";
if (str1.CompareExt(str2) == 0)
{
...
}
Description
Returns the first element alphabetically. If they are the same, returns an empty string.
Parameters
compareTo (string) - The string to compare
Usage
string str1 = "Apples";
string str2 = "Bananas";
str1.CompareFirstExt(str2)
Returns Apples.
Description
Maps to string.CompareOrdinal
Returns integer indicating which comes first in the sort order. If the compareTo value comes first, returns 1. If the compareToValue doesn't come first returns -1. If they are equal, returns 0.
There is also a version where you can pass in an index for each string and a length. For more information see Microsoft's documentation for string.CompareOrdinal.
Parameters
compareTo (string) - The string to compare
Usage
string str1 = "Apples";
string str2 = "Bananas";
str1.CompareOrdinalExt(str2)
Returns -1.
Description
Concatenate the specified text and n number of strings. Can be a list of strings or an array
Similar to string.Concat.
Returns a concatenated string which includes the target and any additional strings.
Parameters
toAdd (params string[]) - one or more strings
Usage
string str1 = "fu";
string str2 = "bar";
string str3 = " star";
string str4 = str1.ConcatExt(str2, str3);
str4 equals fubar star.
Description
Maps to string.Copy
Deep copy.
Parameters
None
Usage
string str1 = "fubar";
string str2 = str1.CopyExt();
str2 equals fubar.
Description
Maps to string.Format
Checks that paramaters not null. If null, returns original value.
Parameters
values (params object[])
Usage
string str1 = "fu{0}{1}";
string str2 = "bar";
string str3 = " star";
string str4 = str1.FormatExt(str2, str3);
str4 equal fubar star.
Description
Maps to string.Intern
Returns the Intern
Parameters
None
Usage
string str1 = "test";
string str2 = str1.InternExt();
Description
Maps to string.IsInterned
Returns the Intern.
Parameters
None
Usage
string str1 = "test";
string str2 = str1.IsInternExt();
Description
Maps to string.Join
Returns the Intern.
Parameters
separator (string)
value (string[])
Overload:
startIndex (int)
length (int)\
Usage
string str1 = "fu";
string str2 = "bar";
string separator = " ";
string newString = str1.JoinExt(separator, str2);
newString equals fu bar
Description
Maps to string.ReferenceEquals
Determines if two strings are the same instance.
Parameters
compareTo (string)
Usage
string str1 = "test";
string str2 = str1;
bool sameRef = str1.ReferenceEqualsExt(str2);
sameRef equals True.
Description
Maps to Convert.ToDateTime
Converts a string to a DateTime.
Parameters
None
Usage
string dateString = "01/01/2018";
DateTime date = dateString.ToDateTimeExt();
Description
Maps to Convert.FromBase64StringExt
Converts a base64 string to a byte array.
Parameters
None
Usage
string str1 = "AAEPISo=";
byte[] bytes = Convert.FromBase64String(str1);
Description
Converts a base64 string into a standard string using default encoding.
Parameters
None
Usage
string str1 = "AAEPISo=";
string b64String = str1.FromBase64String2Ext();
Description
Maps to TitleInfo.ToTitleCase
Converts string to title cased string.
Parameters
None
Usage
string str1 = "AAEPISo=";
byte[] b64 = Convert.FromBase64String(str1);
string str1 = "fubar";
string str2 = str1.ToTitleCaseExt();
str2 equals Fubar.
Description
Reverses a string
Parameters
None
Usage
string str1 = "fubar";
string str2 = str1.ReverseExt();
str2 equals rabuf.
Description
Returns a bool indicating if the text is a palindrome
Parameters
None
Usage
string str1 = "mom";
bool isPalindrome = str1.IsPalindromeExt();
isPalindrome equals True.
Description
Maps to Console.Write
Parameters
None
Usage
string str1 = "test";
str1.WriteToConsoleExt();
Will print test in the console.
Description
Maps to Console.WriteLine
Parameters
None
Usage
string str1 = "test";
str1.WriteLineToConsoleExt();
Will print test in the console.
Description
Maps to Debug.Write
Parameters
None
Usage
string str1 = "test";
str1.WriteToDebugExt();
Will print test in the debug window.
Description
Maps to Debug.WriteIf
Parameters
None
Usage
string str1 = "test";
str1.WriteIfToDebugExt();
Will write test to the trace listeners in the Listeners collection if a condition is true.
Description
Maps to Debug.WriteLineIf
Parameters
None
Usage
string str1 = "test";
str1.WriteIfLineToDebugExt();
Will write test to the trace listeners in the Listeners collection if a condition is true.
Description
Maps to Encoding.GetBytes
Parameters
None
Usage
string s = "test";
byte[] bytes = s.GetBytesExt();
Description
Maps to Encoding.UTF8.GetBytes
Parameters
None
Usage
string s = "test";
byte[] bytes = s.GetBytesUtf8Ext();
Description
Maps to Encoding.UTF7.GetBytes
Parameters
None
Usage
string s = "test";
byte[] bytes = s.GetBytesUtf7Ext();
Description
Maps to Encoding.UTF32.GetBytes
Parameters
None
Usage
string s = "test";
byte[] bytes = s.GetBytesUtf32Ext();
Description
Maps to Encoding.Unicode.GetBytes
Parameters
None
Usage
string s = "test";
byte[] bytes = s.GetBytesUnicodeExt();
Description
Maps to Encoding.ASCII.GetBytes
Parameters
None
Usage
string s = "test";
byte[] bytes = s.GetBytesASCIIExt();
Description
Maps to Encoding.BigEndianUnicode.GetBytes
Parameters
None
Usage
string s = "test";
byte[] bytes = s.GetBytesBigEndianUnicodeExt();
Description
Maps to System.Web.HttpUtility.UrlEncode
Encodes a URL string.
Parameters
None
Usage
string s = "This & That";
string encodedString = s.UrlEncodeExt();
Result: This+%26+That
Description
Maps to System.Web.HttpUtility.UrlDecode
Decodes a URL string.
Parameters
None
Usage
string s = "This+%26+That";
string decodedString = s.UrlDecodeExt();
Result: This & That
Description
Maps to System.Web.HttpUtility.htmlEncode
Converts a string to an html encoded string.
Parameters
None
Usage
string s = "This & That";
string htmlString = s.HtmlEncodeExt();
Result: This & That
Description
Maps to System.Web.HttpUtility.Htmldecode
Converts a string from an html encoded string.
Parameters
None
Usage
string s = "This & That";
string decodedString = s.HtmlDecodeExt();
Result: This & That
Description
Maps to System.Web.HttpUtility.HtmlAttributeEncode
Parameters
None
Usage
string s = "This & That";
string encodedAttrString = s.HtmlAttributeEncodeExt();
Result: This & That
Description
Maps to System.Web.HttpUtility.JavaScriptStringEncode
Parameters
None
Usage
string s = "<script>function {};</script>";
string jsEncoded = s.JavaScriptStringEncodeExt();
Result: \u003cscript\u003efunction {};\u003c/script\u003e
Description
Maps to System.Web.HttpUtility.UrlEncodeToBytes
Parameters
e (Encoding) - Optional
Usage
string s = "This & That";
byte[] bytes = s.UrlEncodeToBytesExt();
Description
Maps to System.Web.HttpUtility.UrlDecodeToBytes
Parameters
e (Encoding) - Optional
Usage
string s = "This+%26+That";
byte[] bytes = s.UrlDecodeToBytesExt();
Description
Capitalizes the first character in every sentence of a string. A sentence is defined by a string fragment that terminates in a period or a string with no periods. If the string is null or empty, returns the original string.
Parameters
None
Usage
string s = "this is a test string.";
string capitalized = s.CapitalizeExt();
capitalized equals "This is a test string."
string s2 = "this is a test string. this is another test string.";
string capitalized2 = s2.CapitalizeExt();
capitalized2 equals "This is a test string. This is another test string."
Description
Duplicates the value of a string the specified number of times.
String can be of any valid string value including a character, word, sentence, empty space, white space, or null.
Useful for adding padding.
Parameters
length (int)
Usage
string s = "s";
string sDuplicated = s.DuplicateExt(5);
sDuplicated equals "sssss"
Description
Centers the string and adds leading and trailing characters.
Optional char parameter specifies the fill in the beginning and the end.
If the word is larger then the specified length, the original word is returned.
Parameters
length (int)
fill (char) - Optional
Usage
string test = "test";
string centered = test.CenterExt(14);
string centered2 = test.CenterExt(14, 'a');
centered equals " test " centered2 equals "aaaaatestaaaaa"
Description
Returns true if all characters inside the string are either numbers or letters, otherwise returns false. All characters evaluated including blank spaces.
Parameters
None
Usage
string text = "abc123";
if (text.IsAlphaNumericExt())
{
// Do Something...
}
Description
Returns true if all characters inside the string are letters, otherwise returns false. All characters evaluated including blank spaces.
Parameters
None
Usage
string text = "abc";
if (text.IsAlphaExt())
{
// Do Something...
}
Description
Returns true if all characters inside the string are numeric, otherwise returns false. All characters evaluated including blank spaces. A decimal number returns false since it contains a non-numeric character (period).
Parameters
None
Usage
string text = "123";
if (text.IsNumericExt())
{
// Do Something...
}
Description
Returns true if all characters in the string are within the ASCII number range (0-126), otherwise returns false.
Parameters
None
Usage
string text = "æ";
if (!text.IsASCIIExt())
{
Console.WriteLine("Non-ASCII characters are not permitted.");
}
Description
Returns true if all characters in the string are letters and lower case, otherwise returns false. Null, empty string, and white space return false.
Parameters
None
Usage
text = "zzz";
if (text.IsLowerExt())
{
Console.WriteLine("All characters are lowercase.");
}
Description
Returns true if all characters in the string are letters and upper case, otherwise returns false. Null, empty string, and white space return false.
Parameters
None
Usage
text = "AAA";
if (text.IsUpperExt())
{
Console.WriteLine("All characters are uppercase.");
}
Description
Returns true if the string matches the regular expression pattern parameter, otherwise returns false.
Maps to Regex.IsMatch
Parameters
pattern (string)
options (RegexOptions) - Overload
matchTimeout (TimeSpan) - Overload
Usage
string text = "Lorem ipsum dolor sit amet consectetur adipiscing elit";
string pattern = "^[a-zA-Z.\\s]+$";
if (text.IsMatchExt())
{
Console.WriteLine("text matches pattern.");
}
Description
Converts a string to a Uri.
If the supplied string is not a valid uri, with throw a System.UriFormatException.
Parameters
None
Usage
string url = "https://www.example.com";
Uri uri = url.ToUriExt();
Description
Creates an HttpWebRequest from the string.
Parameters
method (string) - Optional
contentType (string) - Optional
accept (string) - Optional
host (string) - Optional
timeout (int) - Optional
mediaType (string) - optional
userAgent (string) - optional
headers (Tuple<string,string>) - Optional
Usage
string url = "https://www.example.com";
HttpWebRequest request = url.ToHttpWebRequest();
Description
Converts a standard string into a base64 encoded string.
Parameters
None
Usage
string text = "Lorem ipsum dolor amet";
string text64 = text.ToBase64StringExt();
Description
From MSDN: Writes a message followed by a line terminator to the trace listeners in the Listeners collection.
Maps to Debug.Print
Parameters
None
Usage
string text = "test";
text.PrintExt();
Description
Writes an error message to the trace listener.
Maps to Trace.TraceError
Parameters
None
Usage
string message = "An error occurred.";
message.TraceErrorExt();
Description
Writes a warning message to the tace listener.
Maps to Trace.TraceWarning
Parameters
None
Usage
string message = "Warning!";
message.TraceWarningExt();
Description
Write an information message to the trace listener.
Maps to Trace.TraceInformation
Parameters
None
Usage
string message = "File opened...";
message.TraceInformationExt();
Description
Returns a string as Xml from the extended string.
Root name is optional. If not supplied to the method, the root element will be Root.
Namespace is optional.
Parameters
root (string) Default value is "root"
ns (string) Default value is ""
Usage
string text = "Some text to create XML.";
string root = "Name";
string nameSpace = "https://www.namespace.com";
string xmlString = text.ToXmlExt(root, nameSpace);
Description
Returns an XDocument from the extended string.
Root name is optional. If not supplied to the method, the root element will be Root.
Namespace is optional.
Parameters
root (string) Default value is "root"
ns (string) Default value is ""
Usage
string text = "Some text to create XML.";
string root = "Name";
string nameSpace = "https://www.namespace.com";
XDocument xDocument = text.ToXDoumentExt(root, nameSpace);
Description
Rearranges the letters in a string. Letters are shuffled within word groupings, so if a string has 3 word groupings it will remain 3 word groupings.
Parameters
None
Usage
string text = "abcdefghijklmnop";
string shuffled = text.ShuffleExt();
Description
Replaces text with a dummy character for storing sensitive data.
Contains two optional parameters.
The first parameter character is the char used to replace the text. If not provided, it will use an asterisk.
The second parameter length is an int that specifies how many characters you want to replace.
If left blank or a negative int is passed in will replace the entire string.
Parameters
length (int?)
Usage
string text = "P@ssw0rd#";
string scrubbed = text.ScrubExt(); // "*********";
string scrubbed2 = text.ScrubExt(length: 5); // "P@ss*****";
string scrubbed3 = text.ScrubExt('#', 8); // "P########";
Description
Tabs the extended string the number of times specified in the numTabs parameter.
Parameters
numOfTabs (int)
Usage
string text = "Lorem ipsum dolor amet.";
string tabbed = text.TabExt(5);
Description
Added the number of line breaks specified in the paramater below the extended string.
Parameters
numOfLines (int)
Usage
string text = "Lorem ipsum dolor amet.";
string tabbed = text.LineBreakExt(3);
Description
Returns true if the string is valid JSON, otherwise returns false.
Parameters
None
Usage
string text = "{\"name\": \"Bob\"}";
if (text.IsJsonExt())
{
var person = JsonSerializer.Deserialize<Person>(text)!;
}