Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
Working with strings is one of the most common—yet performance-sensitive—tasks in .NET development. The StringExtensions class in the DotNetTips.Spargine.Extensions project and NuGet package delivers a powerful, high-performance toolkit designed to make string manipulation faster, safer, and easier. From hashing and validation to encoding, parsing, compression, and zero-allocation helpers, these extensions streamline everyday operations while reducing boilerplate code and potential runtime errors.
Whether you’re building APIs, processing user input, securing data, or optimizing performance-critical paths, StringExtensions gives you a comprehensive and reliable set of utilities to handle complex string operations with clean, readable syntax and excellent performance.
Methods
Below is a detailed overview of some key methods, categorized based on their functionality:
String Combination and Concatenation
- CombineToString(string input, params string[] args)
Merges an array of strings into a single string. - Concat(string input, string delimiter, bool addLineFeed, params ReadOnlyCollection<string> args)
Concatenates a series of strings using a specified delimiter, with an option to add a line feed after each string.
Hashing Functions
- ComputeHash(string input, HashType hashType)
Computes the hashType of the given base64Input string using the specified hashType algorithm. - ComputeSHA256Hash()
Generates the SHA256 hash of the input string. - HashPassword(string input, HashAlgorithmType algorithmType)
Hashes the base64Input string using the specified hashing algorithm. - VerifyHashedPassword(string hashedPassword, string input, HashAlgorithmType algorithmType)
Verifies the hashed password against the base64Input string using the specified hashing algorithm.
Default Value Handling
- DefaultIfNull(string input)
Returns the original string if not null, otherwise an empty string. An overload allows for specifying a custom default value. This method is overloaded to return a default string. - DefaultIfNullOrEmpty(string input, string defaultValue)
Returns the original string unless it is null or empty, in which case a specified default value is returned.
String Splitting and Joining
- DelimitedStringToArray(string input, char delimiter)
Splits a delimited string into an array, automatically removing empty entries. - Split(string input, StringSplitOptions options, char delimiter)
Splits a string into substrings using a single character delimiter and returns them as a read-only collection. This method is overloaded to use a count that specifies the maximum number of substrings to return, as well as a custom separator. - SplitLines(string input)
Splits a multi-line string into individual lines using a zero-allocation enumerator pattern. - SplitRemoveEmpty(string input)
Splits a comma-delimited string into a read-only collection of non-empty substrings.
String Matching and Comparison
- ContainsAny(string input, StringComparison stringComparison, params string[] characters)
Determines whether the input string contains any specified characters, with a custom comparison option. - EqualsIgnoreCase(input, string inputToCompare))
Compares two strings for equality, ignoring case and using the specified comparison option. - EqualsOrBothNullOrEmpty(string input, string inputToCompare)
Checks if the specified string is either equal to the current string or if both are null/empty. - FastEquals(string value, string valueToCompare, StringComparison comparison)
Compares two strings using the specified string comparison option. - FastIsNullOrEmpty(string input)
Determines whether the specified string is null or empty in a fast manner. - StartsWithOrdinal(string input, string inputToCompare)
Determines whether the beginning of this string instance matches the specified string when compared using the ordinal sort order. - StartsWithOrdinalIgnoreCase(string input, string inputToCompare)
Determines whether the beginning of this string instance matches the specified string when compared using the ordinal sort order and ignoring case.
Extraction and Trimming
- Extract(string input, string start, string end)
Extracts a substring from the input string that is located between the specified start and end delimiter strings. - SubstringTrim(string input, int startIndex, int length)
Extracts a substring from the specified range of the string and trims leading and trailing whitespace from the result. - ToTrimmed(string input)
Trims the specified string, removing all leading and trailing white-space characters.
Encoding and Compression
- FromBase64(string base64Input)
Decodes a Base64-encoded string back into its original form. - FromBrotliStringAsync(), FromDeflateStringAsync(), FromGZipStringAsync(), FromZLibStringAsync()
Decompresses a string compressed with Brotli, Deflate, GZip, or ZLib asynchronously. - ToBase64(string input)
Encodes the input string into its Base64 representation. - ToBase64Bytes(string input)
Converts a Base64 encoded string to a byte array. - ToBrotliStringAsync(), ToDeflateStringAsync(), ToGZipStringAsync(), ToZLibStringAsync()
Compresses a string using Brotli, Deflate, GZip, or ZLib algorithms asynchronously, returning the compressed result. - ToByteArray(string input, Encoding encoding)
Converts the specified string to a byte array using the provided encoding. - ToByteArrayFromBase64(string base64String)
Converts a Base64 encoded string to a byte array.
Validation and Identification
- IsCreditCardNumber(string input)
Validates if the string follows a credit card number format. - IsCurrencyCode(string input)
Validates whether the string represents a valid currency code. - IsDomainAddress(string input)
Determines if the string is a valid domain address. - IsEmailAddress(string input)
Validates whether a string is a valid email address. - IsEmpty(string? input)
Determines whether the specified string is empty. - IsFirstLastName(string input)
Validates if the string contains a valid first and last name. - IsGuid(string input)
Determines if the input string is a valid GUID. - IsISBN(string input)
Confirms if the string is a valid International Standard Book Number (ISBN). - IsMacAddress(string input)
Checks if the string is a valid MAC address. - IsNotEmpty(string? input)
Determines whether the specified string is not empty. - IsOneToSevenAlpha(string input)
Determines whether the specified string contains only characters in the range of ‘1’ to ‘7’ and is alphabetic. - IsScientific(string input)
Determines whether the specified string is in scientific notation. - IsStringSHA1Hash(string input)
Determines whether the specified string is a valid SHA1 hash type. - IsUrl(string input)
Checks if the string is a valid URL. - IsValidString(string input)
Determines whether the specified string matches the pattern defined in the regular expression: “^(\w|\W)+$”.
Miscellaneous
- CalculateByteArraySize(string input, Encoding? encoding = null)
Calculates the size of the byte array that will result from encoding the string using the specified encoding. - CalculateTotalLength(string[] args)
Calculates the total character count from an array of strings. - FastIsNullOrEmpty(string input)
Determines whether the specified string is null or empty in a fast manner. - FastParseUrl(string url)
Parses a URL string into its constituent components: scheme, host, port, and path. - FastReplace(string input, string oldValue, string newValue)
Performs a fast, culture-invariant string replacement by replacing all occurrences of a specified string with another string. - HasValue(string input)
Checks if the string is neither null nor whitespace. The method is overloaded to accept a length, StringComparison, regular expression, or range of minimum and maximum lengths. - HasWhitespace(string input)
Determines whether the specified string contains any whitespace characters. - Indent(string input, in int length, char indentationCharacter)
Adds indentation characters to the beginning of the input string. - RemoveCRLF(string input, string replacement)
Removes all carriage return and line feed characters from the specified string. - ReplaceEllipsisWithPeriod(string input)
Replaces occurrences of an ellipsis (…) with a period (.). - ToTitleCase(string input)
Converts the specified string to title case, considering culture-specific rules.
Performance Optimizations
Each method in the StringExtensions class is optimized for high performance, providing developers with efficient, reliable string manipulation tools essential for building robust .NET applications.
This collection of methods ensures developers have access to a comprehensive and efficient toolkit for handling string operations across various scenarios, from basic concatenation to complex validation and compression.
Summary
The StringExtensions class in DotNetTips.Spargine.Extensions provides an extensive library of high-performance utilities for working with strings in .NET. These methods cover a wide range of scenarios—including combination and formatting, hashing and security, Base64 and compression handling, validation, parsing, trimming, comparison, and performance-optimized helpers. Designed with both productivity and reliability in mind, these extensions help developers write cleaner code while improving execution efficiency and reducing common string-handling pitfalls. If your applications work heavily with string data, Spargine’s StringExtensions offers a robust toolkit that can significantly enhance code quality, maintainability, and runtime performance.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

