IvanStoychev.Useful.String.Extensions 2.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package IvanStoychev.Useful.String.Extensions --version 2.0.0
NuGet\Install-Package IvanStoychev.Useful.String.Extensions -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IvanStoychev.Useful.String.Extensions" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IvanStoychev.Useful.String.Extensions --version 2.0.0
#r "nuget: IvanStoychev.Useful.String.Extensions, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install IvanStoychev.Useful.String.Extensions as a Cake Addin
#addin nuget:?package=IvanStoychev.Useful.String.Extensions&version=2.0.0

// Install IvanStoychev.Useful.String.Extensions as a Cake Tool
#tool nuget:?package=IvanStoychev.Useful.String.Extensions&version=2.0.0

Project logo

Source and debug symbols embedded

(You can step through this package's code during debug)

What is this?

A library of extension methods for the string class that make work with text easier.

How do I get started?

Add using IvanStoychev.Useful.String.Extensions; And call methods on any string.


Video Demonstration

(Will open in YouTube)

Demonstration video

Examples:

Remove all occurrences of a collection of strings
string testString = "To buy: Eggs. eggs. Jam. Ham. Milk. ham. Bread. Bread";
List<string> stringsToRemove = new() { "Eggs. ", "Ham. ", ". Bread" };
string outputDefault = testString.Remove(stringsToRemove);
string outputIgnoreCase = testString.Remove(stringsToRemove, StringComparison.InvariantCultureIgnoreCase);
Console.WriteLine(outputDefault);
Console.WriteLine(outputIgnoreCase);

// Result
// ------
// outputDefault    = "To buy: eggs. Jam. Milk. ham"
// outputIgnoreCase = "To buy: Jam. Milk"

.

Replace all occurrences of a collection of strings with another string
string testString = "Gold. Gems. Jewels. Trinkets. More gems. More jewels!";
List<string> oldStrings = new() { "Gems", "Jewels", "Trinkets" };
string outputDefault = testString.Replace("gold", oldStrings);
string outputIgnoreCase = testString.Replace("gold", oldStrings, StringComparison.InvariantCultureIgnoreCase);
Console.WriteLine(outputDefault);
Console.WriteLine(outputIgnoreCase);

// Result
// ------
// outputDefault    = "Gold. gold. gold. gold. More gems. More jewels!"
// outputIgnoreCase = "Gold. gold. gold. gold. More gold. More gold!"

.

Select everything between two substrings
string testString = "We start on Monday and we finish on Tuesday.";
string startString = "start on ";
string endString = " and we finish";
string outputDefault = testString.Substring(startString, endString);
string outputFull = testString.Substring(startString, endString, StringInclusionOptions.IncludeAll);
Console.WriteLine(outputDefault);
Console.WriteLine(outputFull);

// Result
// ------
// outputDefault = "Monday"
// outputFull    = "start on Monday and we finish"

.

Select everything after/before a substring
string testString = "In the beginning there was bread. In the end there was jam.";
string startString = "bread. ";
string endString = " In the end";
string outputStart = testString.SubstringStart(endString);
string outputEnd = testString.SubstringEnd(startString);
Console.WriteLine(outputStart);
Console.WriteLine(outputEnd);

// Result
// ------
// outputStart = "In the beginning there was bread."
// outputEnd   = "In the end there was jam."

.

All methods have a detailed summary. For a complete list of all methods in the library and details on them consult the wiki.

Feel free to submit any issues or bugs to the project's GitHub issues page. You are welcome to follow my projects twitter. Follow @ivan_stoychev

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 437 12/2/2023
4.0.0 608 3/17/2023
3.0.0 788 10/10/2022
2.0.1 886 6/27/2022
2.0.0 819 6/26/2022
1.0.0 764 9/19/2021

🟢 Updated to .Net 6.
🟢 Updated NuGet Icon.
🟢 Added NuGet README.
🟢 Improved all method validation and thrown exception messages. Now any problems will be caught better and any exceptions caused will contain more useful information.
🟢 Embedded debug symbols, so that the library source code can be stepped through during debugging.
🟢 Added `Remove(string removeString, StringComparison stringComparison = StringComparison.CurrentCulture)` method that removes all occurrences of the given `removeString`.
🟢 Added "Trim(string trimString, StringComparison stringComparison = StringComparison.CurrentCulture, bool trimWhitespace = false)" and "Trim(string trimString, bool ignoreCase, CultureInfo? culture, bool trimWhitespace = false)" methods that remove leading and tailing occurrences of the given "trimString".

🟡 Improved some summaries.
🟡 Fixed "KeepOnlySpecialCharacters()" method only keeping a single instance of each special character.
🟡 Consolidated the "Replace(IEnumerable<string> oldStrings, string newString)" and "Replace(IEnumerable<string> oldStrings, string newString, bool ignoreCase)" methods into "Replace(string newString, IEnumerable<string> oldStrings, StringComparison stringComparison = StringComparison.CurrentCulture)". This allows more string comparison flexibility and features improved validation of arguments and information in thrown exceptions.
🟡 Consolidated the "Replace(IEnumerable<string> oldStrings, string newString)" and "Replace(IEnumerable<string> oldStrings, string newString, bool ignoreCase)" methods into "Replace(string newString, IEnumerable<string> oldStrings, StringComparison stringComparison = StringComparison.CurrentCulture)". This allows more string comparison flexibility.
🟡 Consolidated the "Remove(IEnumerable<string> removeStrings)" and "Remove(bool ignoreCase, IEnumerable<string> removeStrings)" methods into "Remove(IEnumerable<string> removeStrings, StringComparison stringComparison = StringComparison.CurrentCulture)".
🟡 Changed signature of method "Replace(bool ignoreCase, string newString, params string[] oldStrings)" to "Replace(string newString, bool ignoreCase = false, params string[] oldStrings)".
🟡 Added additional information in ArgumentNullExceptions about the name of the method the exception occurred in.
     Before the message would be akin to "The argument given for 'keychars' was null.", now the message will be akin to "The argument given for parameter "keychars" of method "Contains" was null.".
     This was done to give more details to developers trying to debug the source of such an exception, as many different methods can have parameters with the same name.
🟡 Added boolean "trimWhitespace" parameter and changed parameter "culture" of methods "TrimStart(string trimString, bool ignoreCase, CultureInfo culture)" and "TrimEnd(string trimString, bool ignoreCase, CultureInfo culture)" to be nullable. The signatures are now "TrimStart(string trimString, bool ignoreCase, CultureInfo? culture, bool trimWhitespace = false)" and "TrimEnd(string trimString, bool ignoreCase, CultureInfo? culture, bool trimWhitespace = false)".

🔴 Removed methods "Replace(string newString, params string[] oldStrings)", "Replace(bool ignoreCase, string newString, params string[] oldStrings)", "Contains(params string[] keywords)", "Contains(StringComparison comparison, params string[] keywords)", "Contains(params char[] keychars)", "Contains(StringComparison comparison, params char[] keychars)", "Remove(params string[] removeStrings)" and "Remove(bool ignoreCase, params string[] removeStrings)" as they could be, legally, called with no argument for the "params" parameter, which would either do nothing or throw an exception. This is a very confusing and unpleasant behaviour, even when fully documented. It was decided that the convenience of, potentially, saving the user from passing these arguments as an "IEnumerable" is not worth the hassle.
🔴 Removed methods "Trim(int amount)", "TrimStart(int amount)" and "TrimEnd(int amount)" as the same functionality can be easily achieved with indices and ranges (https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/ranges-indexes) or the "Substring" method.
🔴 Removed methods "TrimStart(string trimString)" and "TrimEnd(string trimString)" and added default value for parameter "stringComparison" of methods "TrimStart(string trimString, StringComparison stringComparison)" and "TrimEnd(string trimString, StringComparison stringComparison)".


Legend:
🟢 - New feature
🟡 - Altered existing feature
🔴 - Removed feature