A string manipulation library for C#, built around three principles: correctness, performance, and a minimal API surface.
- Zero-allocation where possible. Most methods operate on
Span<char>/ReadOnlySpan<char>internally, usingstackallocfor small inputs andArrayPool<char>for larger ones instead of allocating a new array on every call. - Unicode-correct. Methods that classify or compare characters operate on whole Unicode scalar values (via
Rune) rather than raw UTF-16 code units, so characters outside the Basic Multilingual Plane (many emoji, some rare scripts) are handled correctly instead of being split into invalid half-characters. - Both
stringandReadOnlySpan<char>overloads. Every method that accepts astringalso has aReadOnlySpan<char>overload, so you can call it on a slice without allocating an intermediate string.
.NET 10.0 or later.
dotnet add package String-ExtensionMethods are split across namespaces by category. Add the relevant using statement for the methods you need.
General-purpose string manipulation.
| Method | Description |
|---|---|
RemoveCharacters(char[]) |
Removes the specified characters from a string. |
CountSubstring(string) |
Counts the number of occurrences of a substring. |
ReverseWords() |
Reverses the order of words in a string. |
RemoveDuplicateCharacters() |
Removes duplicate characters, keeping the first occurrence of each. |
Format validation.
| Method | Description |
|---|---|
IsValidEmail() |
Validates an e-mail address. |
IsValidPhoneNumber() |
Validates a phone number. |
Text analysis.
| Method | Description |
|---|---|
IsPalindrome() |
Determines if a string is a palindrome. |
CountLetters() |
Counts the number of letters in a string. |
Casing conventions and slug generation.
| Method | Description |
|---|---|
ToCamelCase() |
Converts to camelCase. |
ToPascalCase() |
Converts to PascalCase. |
ToSnakeCase() |
Converts to snake_case. |
ToKebabCase() |
Converts to kebab-case. |
ToTitleCase(bool) |
Converts to Title Case, with an optional flag to keep short English words (of, the, and, ...) lowercase. |
Slugify(char) |
Converts to a URL-friendly slug, stripping accents and collapsing punctuation into a single separator. |
using StringExtension;
using StringExtension.Casing;
using StringExtension.Linguistics;
using StringExtension.Validation;
"hello world!".RemoveCharacters(['l', 'o']); // "he wrd!"
"hello world!".ReverseWords(); // "world! hello"
"john.doe@example.com".IsValidEmail(); // true
"racecar".IsPalindrome(); // true
"hello_world".ToCamelCase(); // "helloWorld"
"helloWorld".ToSnakeCase(); // "hello_world"
"the lord of the rings".ToTitleCase(useEnglishMinorWordRules: true);
// "The Lord of the Rings"
"Café de la Gare! 2024".Slugify(); // "cafe-de-la-gare-2024"Every method that returns a string avoids unnecessary intermediate allocations. As an example, here is the impact of the zero-allocation rewrite of a few methods (measured with BenchmarkDotNet on identical hardware, before and after):
| Method | Before | After | Allocated (before → after) |
|---|---|---|---|
IsPalindrome |
466 ns | 1.6 ns | 648 B → 0 B |
CountSubstring |
59 ns | 1.6 ns | 40 B → 0 B |
CountLetters |
101 ns | 3.7 ns | 32 B → 0 B |
Run the benchmarks yourself:
dotnet run -c Release --project Benchmarks/Benchmarks.csproj- Unit tests:
UnitTests/StringExtensionTests.cs - Performance tests:
Benchmarks/Benchmark.cs
dotnet testContributions are welcome, whether that's proposing new methods, reporting bugs, or improving existing code. Open a Pull Request and it will be reviewed.
Distributed under the MIT License. See LICENSE for details.