c# StringExtensions Library provides comprehensive string extension methods that go beyond common string validation, extending System.String. It was motivated by the lack of a StringUtils-style library such as org.apache.commons.lang3.StringUtils in .NET.
The library targets .NET Standard 2.0, so it works with modern .NET 8 / 9 / 10 apps as well as .NET Core 2.0+ and .NET Framework 4.6.1+. Licensed under GPL-3.0-or-later. See CHANGELOG.md for 2.0.0 breaking changes and CONTRIBUTING.md for the null-handling policy.
Version 2.0.0 is intended for production use as a string helper library within these limits:
- License: GPL-3.0-or-later (copyleft). Proprietary products must comply with GPL terms.
- Email / IPv4: practical checks, not full RFC 5322 or every IPv4 textual form.
IsValidIPv4requires a canonical dotted-quad. - Encrypt / Decrypt: passphrase-based AES-256-CBC with HMAC-SHA256 and PBKDF2-HMAC-SHA256 (100,000 iterations). Not a key-management system. 1.x RSA payloads will not decrypt.
- JSON: Newtonsoft.Json 13.x.
JsonToObjectthrows if deserialize is null. - SQL: there is no SQL-building API. Do not concatenate untrusted strings into queries.
Install-Package StringExtensionsLibrary
Once you have installed the String extension library within your project. String extensions functions will be available on all string types
if("64.233.161.1470".IsValidIPv4()){
\\do something
}
| Function Name | Description |
|---|---|
| IsDateTime | Checks if date with dateFormat is parse-able to System.DateTime format returns boolean |
| ToInt32 | Converts the string representation of a number to its 32-bit signed integer equivalent |
| ToInt64 | Converts the string representation of a number to its 64-bit signed integer equivalent |
| ToInt16 | Converts the string representation of a number to its 16-bit signed integer equivalent |
| ToDecimal | Converts the string representation of a number to its System.Decimal equivalent |
| ToBoolean | Converts string to its boolean equivalent |
| ToBytes | Convert a string to its equivalent UTF-16 little-endian byte array (Buffer.BlockCopy of chars; not UTF-8). Hash helpers use the same encoding |
| SplitTo | Returns an enumerable collection of the specified type containing the substrings in this instance that are delimited by elements of a specified Char array |
| ToEnum | Converts string to its Enum type,,Checks if string is a member of type T enum before converting. if fails returns default enum |
| Format | Extension string.Format helper ("{0}".Format(arg)). Distinct from the static string.Format overloads |
| GetEmptyStringIfNull | Gets empty String if passed value is of type Null |
| GetDefaultIfEmpty | Returns a default String value if given value is null or empty |
| IsInteger | IsInteger Function checks if a string is a valid int32 value |
| IsNumeric | Checks if a string is a valid floating value |
| IsAlpha | Checks if String contains only Unicode letters |
| IsAlphaNumeric | Checks if the String contains only Unicode letters & digits. |
| IsValidIPv4 | Checks if a string is a canonical dotted-quad IPv4 address |
| IsEmailAddress | Practical email check (plus-tags allowed). Not RFC 5322-complete |
| Truncate | Truncate String and appends trailing ... |
| Capitalize | Reads in a sequence of words from standard input and capitalize each,one (make first letter uppercase; make rest lowercase |
| FirstCharacter | Gets the first character in string |
| LastCharacter | Gets last character in string |
| Replace | Delete listed characters (params char[]). Distinct from string.Replace(char, char), which substitutes a replacement character |
| RemoveChars | Remove Characters from string |
| Reverse | Reverse string |
| ParseStringToCsv | Escapes string by appending quotes for csv output |
| Encrypt | Encrypt a string using AES-256-CBC and HMAC-SHA256 (passphrase-derived key) |
| Decrypt | Decrypt a string produced by Encrypt; wrong key or tampered payload throws CryptographicException |
| CountOccurrences | Count number of occurrences in string based on the string to match |
| JsonToDictionary | Converts a Json string to dictionary object. function is only applicable for single hierarchy objects i.e no parent child relationships, for parent child relationships JsonToExpanderObject |
| JsonToExpanderObject | Converts a Json string to ExpandoObject method applicable for multi hierarchy objects i.e,having zero or many parent child relationships |
| JsonToObject | Converts a Json string to object of type T. function applicable for multi hierarchy objects i.e,having zero or many parent child relationships, Ignore loop references and do not serialize if cycles are detected. |
| RemovePrefix | Removes the first part of the string, if no match found return original string |
| RemoveSuffix | Removes the end part of the string, if no match found return original string |
| EndsWithIgnoreCase | Check a String ends with another string ignoring the case. |
| StartsWithIgnoreCase | Check a String starts with another string ignoring the case. |
| DoesNotStartWith | Check if a string does not start with prefix |
| DoesNotEndWith | Check if a string does not end with prefix |
| AppendSuffixIfMissing | Appends the suffix to the end of the string if the string does not already end in the suffix |
| AppendPrefixIfMissing | Appends the prefix to the start of the string if the string does not already start with prefix |
| CreateHashSha512 | Convert string to Hash using Sha512 |
| CreateHashSha256 | Convert string to Hash using Sha256 |
| QueryStringToDictionary | Convert a URL query string to an IDictionary; keys and values are URL-decoded (+ as space) |
| ReverseSlash | Reverse back or forward slashes |
| ReplaceLineFeeds | Remove CR/LF sequences; other characters (including .) are unchanged |
| GetByteSize | Calculates the amount of bytes occupied by the input string based on the specified encoding argument |
| Left | Extracts the left part of the input string limited by the length argument |
| Right | Extracts the right part of the input string limited by the length argument |
| ToTextElements | Converts a string to an Enumerable collection type of string elements |
| IsNull | Checks if a string is null |
| IsNullOrEmpty | Instance wrapper around string.IsNullOrEmpty; prefer the static method in new code if both are in scope |
| IsMinLength | Checks if string length is a certain minimum number of characters, does not ignore leading and trailing,white-space.,null strings will always evaluate to false. |
| IsMaxLength | Checks if string length consists of the specified allowable maximum char length |
| IsLength | Checks if string length satisfies minimum and maximum allowable char length. does not ignore leading and,trailing white-space |
| GetLength | Gets the number of characters in string checks if string is null |