A modern .NET library providing essential extension methods for System.String to simplify common string operations. Part of the Daily DevOps & .NET - NetEvolve project.
- 🎯 Simple & Intuitive API - Extension methods that feel natural to use
- 🚀 Multi-Framework Support - Compatible with .NET Standard 2.0, .NET 8.0, .NET 9.0, and .NET 10.0
- 📦 Minimal Dependencies - Single, lightweight external dependency (
NetEvolve.Arguments) - 🔒 Null-Safe - Proper argument validation with meaningful exceptions
Install the package via NuGet Package Manager:
dotnet add package NetEvolve.Extensions.StringsOr via the Package Manager Console:
Install-Package NetEvolve.Extensions.StringsEnsures that a string ends with the specified suffix. If the string already ends with the suffix, the original string is returned; otherwise, the suffix is appended.
using System;
string path = "C:\\Users\\Documents";
string result = path.EnsureEndsWith("\\");
// Result: "C:\\Users\\Documents\\"
string url = "https://example.com/";
string result2 = url.EnsureEndsWith("/");
// Result: "https://example.com/" (unchanged, already ends with "/")
// Case-insensitive comparison
string text = "Hello";
string result3 = text.EnsureEndsWith("WORLD", StringComparison.OrdinalIgnoreCase);
// Result: "HelloWORLD"Parameters:
value(string): The string to checksuffix(string): The suffix to ensurecomparison(StringComparison): Optional comparison mode (default:CurrentCulture)
Exceptions:
ArgumentNullException: Thrown ifvalueorsuffixis null
Ensures that a string starts with the specified prefix. If the string already starts with the prefix, the original string is returned; otherwise, the prefix is prepended.
using System;
string path = "Documents\\file.txt";
string result = path.EnsureStartsWith("C:\\");
// Result: "C:\\Documents\\file.txt"
string url = "example.com";
string result2 = url.EnsureStartsWith("https://");
// Result: "https://example.com"
// Case-insensitive comparison
string text = "world";
string result3 = text.EnsureStartsWith("HELLO", StringComparison.OrdinalIgnoreCase);
// Result: "HELLOworld"Parameters:
value(string): The string to checkprefix(string): The prefix to ensurecomparison(StringComparison): Optional comparison mode (default:CurrentCulture)
Exceptions:
ArgumentNullException: Thrown ifvalueorprefixis null
- .NET Standard 2.0
- .NET 8.0
- .NET 9.0
- .NET 10.0
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.