A collection of extension methods for string β substring helpers (Left / Right), center-padding (PadCenter), and case conversions (ToCamelCase / ToKebabCase / ToPascalCase / ToSnakeCase / ToTitleCase).
dotnet add package Wolfgang.Extensions.StringThis project is licensed under the MIT License. See the LICENSE file for details.
- GitHub Repository: https://github.com/Chris-Wolfgang/String-Extensions
- API Documentation: https://Chris-Wolfgang.github.io/String-Extensions/
- CHANGELOG: CHANGELOG.md
- Contributing Guide: CONTRIBUTING.md
- Formatting Guide: docs/README-FORMATTING.md
- Release Workflow Setup: docs/RELEASE-WORKFLOW-SETUP.md
- Workflow Security: docs/WORKFLOW_SECURITY.md
using System;
using Wolfgang.Extensions.String;
// Substring helpers (null-tolerant, length-clamped)
var path = "/usr/local/bin/app";
var first5 = path.Left(5); // "/usr/"
var last3 = path.Right(3); // "app"
// Centered formatting
Console.WriteLine($"{"Name".PadCenter(10)}|{"Date".PadCenter(12)}");
Console.WriteLine("----------+------------");
Console.WriteLine($"{"Steve",-10}|{"04/13/2026".PadCenter(12)}");
// Case conversions
var input = "user login count";
Console.WriteLine(input.ToCamelCase()); // "userLoginCount"
Console.WriteLine(input.ToKebabCase()); // "user-login-count"
Console.WriteLine(input.ToPascalCase()); // "UserLoginCount"
Console.WriteLine(input.ToSnakeCase()); // "user_login_count"
Console.WriteLine(input.ToTitleCase()); // "User Login Count"The table below is a snapshot of the public API at the time of writing. For the authoritative list (kept in sync with source on every release), see the API documentation.
| Method | Returns | Description |
|---|---|---|
Left(this string, int length) |
string |
Returns the leftmost length characters. Returns the whole string when it is shorter than length; returns null for a null input. |
Right(this string, int length) |
string |
Returns the rightmost length characters. Returns the whole string when it is shorter than length; returns null for a null input. |
PadCenter(this string, int totalWidth) |
string |
Centers the string within totalWidth, padding both sides with spaces. When padding is uneven the extra character goes on the right. Returns the original string when it is already at least totalWidth long. |
PadCenter(this string, int totalWidth, char paddingChar) |
string |
As above, padding with paddingChar instead of spaces. |
ToCamelCase(this string) |
string |
Converts to camelCase. Separator characters are treated as word boundaries and removed. |
ToKebabCase(this string) |
string |
Converts to kebab-case. Separator characters become dashes. |
ToPascalCase(this string) |
string |
Converts to PascalCase. Separator characters are treated as word boundaries and removed. |
ToSnakeCase(this string) |
string |
Converts to snake_case. Separator characters become underscores. |
ToTitleCase(this string) |
string |
Converts to Title Case, preserving spaces and punctuation. Delegates to TextInfo.ToTitleCase using the current culture. |
Left / Right are length-clamping and null-tolerant. PadCenter and the ToXxxCase methods throw ArgumentNullException for a null input; PadCenter throws ArgumentOutOfRangeException for a negative width. Separator handling uses char.IsSeparator, so control characters and punctuation are preserved.
// Substring helpers
"A sample string".Left(8); // "A sample"
"Hello".Left(10); // "Hello" (shorter than requested β unchanged)
"Hello".Left(0); // ""
"A sample string".Right(6); // "string"
"Hello".Right(10); // "Hello"
"Hello".Right(0); // ""
// Center padding
"Hello".PadCenter(11); // " Hello "
"Hello".PadCenter(11, '-'); // "---Hello---"
"Hello".PadCenter(3); // "Hello" (already wide enough β unchanged)
// Case conversions
"A sample string for processing".ToCamelCase(); // "aSampleStringForProcessing"
"A sample string for processing".ToKebabCase(); // "a-sample-string-for-processing"
"A sample string for processing".ToPascalCase(); // "ASampleStringForProcessing"
"A sample string for processing".ToSnakeCase(); // "a_sample_string_for_processing"
"a sample string for processing".ToTitleCase(); // "A Sample String For Processing"Note:
ToTitleCaseis a general-purpose implementation that delegates toTextInfo.ToTitleCase. Proper names, addresses, and other domain-specific text often require specialized handling (e.g., "McDonald", "van der Berg", "O'Brien") that is not provided here.
This library targets:
- .NET Framework: 4.6.2
- .NET Standard: 2.0
- .NET: 8.0, 10.0
See the NuGet package page for the authoritative per-TFM compatibility matrix.
This project enforces strict code quality standards through 8 analyzer rule sets (7 explicit PackageReferences plus the .NET SDK's built-in Microsoft.CodeAnalysis.NetAnalyzers), a <TreatWarningsAsErrors>true</TreatWarningsAsErrors> Release gate, and a banned-API ruleset:
- Microsoft.CodeAnalysis.NetAnalyzers β Built-in .NET analyzers for correctness and performance
- Roslynator.Analyzers β Advanced refactoring and code quality rules
- AsyncFixer β Async/await best practices and anti-pattern detection
- Microsoft.VisualStudio.Threading.Analyzers β Thread safety and async patterns
- Microsoft.CodeAnalysis.BannedApiAnalyzers β Prevents usage of banned synchronous APIs (see
BannedSymbols.txt) - Meziantou.Analyzer β Comprehensive code quality rules
- SonarAnalyzer.CSharp β Industry-standard code analysis
- Microsoft.CodeAnalysis.PublicApiAnalyzers β Tracks the public API surface via
PublicAPI.Shipped.txt/PublicAPI.Unshipped.txt; surfaces additions/removals at compile time as a breaking-change review gate
- .NET 10.0 SDK β required for the modern build / test / pack flow used by CI
- Optional: PowerShell Core for formatting scripts
# Clone the repository
git clone https://github.com/Chris-Wolfgang/String-Extensions.git
cd String-Extensions
# Restore dependencies
dotnet restore
# Build the solution
dotnet build --configuration Release
# Run tests
dotnet test --configuration Release
# Run the benchmarks (optional)
dotnet run -c Release --project benchmarks/Wolfgang.Extensions.String.Benchmarks -- --filter '*'
# Run code formatting (PowerShell Core)
pwsh ./scripts/format.ps1This project uses .editorconfig and dotnet format:
# Format code
dotnet format
# Verify formatting (as CI does)
dotnet format --verify-no-changesSee docs/README-FORMATTING.md for detailed formatting guidelines.
Contributions are welcome! Please see CONTRIBUTING.md for:
- Code quality standards
- Build and test instructions
- Pull request guidelines
- Analyzer configuration details