FunctionalStringExtensions
is a C# class library that provides a set of extension methods for working with strings.
These extensions are designed to make common string manipulation tasks more convenient and expressive.
Whether you're dealing with default values, asynchronous operations, or executing actions based on string conditions,
these extensions aim to simplify your code and enhance its readability.
To easily integrate the FunctionalStringExtensions library into your project, you can use NuGet Package Manager. NuGet is a package manager for .NET that simplifies the process of adding, removing, and updating libraries in your applications.
After that import the FunctionalStringExtensions
namespace in your code files where you want to use the provided extension methods:
using FunctionalStringExtensions;
This extension method returns the provided default value if the input string is null or empty.
Usage:
string result = input.OrDefault("default value");
Similar to the OrDefault
method, this asynchronous extension returns a default value obtained from a Task<string>
if the input string is null or empty.
Usage:
string result = await input.OrDefaultAsync(Task.FromResult("default value"));
This extension method returns the result of the provided delegate function if the input string is null or empty.
Usage:
string result = input.WhenNullOrEmpty(() => "default value");
Similar to the WhenNullOrEmpty
method, this asynchronous extension returns a value obtained from a Task<string>
returned by the delegate function if the input string is null or empty.
Usage:
string result = await input.WhenNullOrEmptyAsync(async () => await GetDefaultValueAsync());
This extension method executes the provided action if the input string is null or empty.
Usage:
input.OnNullOrEmpty(() => Console.WriteLine("Input is null or empty."));
Similar to the OnNullOrEmpty
method, this asynchronous extension executes a provided task if the input string is null or empty.
Usage:
await input.OnNullOrEmptyAsync(async () => await PerformAsyncAction());
Turns your string in a slug
Usage
var slug = "ICH MUß EINIGE CRÈME BRÛLÉE HABEN".ToSlug();
Parse your string to a Enum value
Usage
public enum FakeEnum
{
Value1,
Value2
}
var result = "Value1".ToEnum<FakeEnum>(); // FakeEnum.Value1
Search for letters (A to Z) in the string
Usage
var result = "abc123def456ghi".OnlyLetters(); //"abcdefghi"
Search for numbers (0 to 9) in the string
Usage
var result = "abc123def456ghi".OnlyNumbers(); //"123456"
Search for characters and numbers (A to Z or 0 to 9) in the string
Usage
var result = "12.8/0';@#!%^&*()a12,9abc".OnlyCharactersAndNumbers(); //"1280a129abc"
Search for especial characters (not A to Z and not 0 to 9) in the string
Usage
var result = "12.8/0';@#!%^&*()a12,9abc".OnlySpecialCharacters(); //"./';@#!%^&*(),"
Parses a query string into a dictionary of key-value pairs.
Usage
var queryString = "?name=JohnDoe&age=30&isMember=true";
var result = queryString.ParseQueryString(autoConvertType: true);
// result will be a dictionary with keys "name", "age", "isMember"
// and corresponding values "JohnDoe", 30, true
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you would like to contribute code, please fork the repository and submit a pull request.
This project is licensed under the MIT License. See LICENSE for more information.