Truncate strings at word boundaries — no cut words, no ugly mid-word breaks.
dotnet add package Philiprehberger.SmartTruncateTruncate strings at word boundaries — no cut words, no ugly mid-word breaks.
dotnet add package Philiprehberger.SmartTruncate
using Philiprehberger.SmartTruncate;
// Basic truncation at word boundary
StringTruncator.Truncate("The quick brown fox jumped", 20);
// => "The quick brown..."
// Custom suffix
StringTruncator.Truncate("The quick brown fox jumped", 20, " [more]");
// => "The quick [more]"
// Text fits — returned as-is
StringTruncator.Truncate("Hello", 20);
// => "Hello"
// No word boundary found — hard truncate
StringTruncator.Truncate("superlongwordwithoutspaces", 10);
// => "superlo..."
using Philiprehberger.SmartTruncate;
StringTruncator.Truncate("Hello wonderful world", 15, " [more]");
// "Hello [more]"
using Philiprehberger.SmartTruncate;
StringTruncator.Truncate("Hi", 10); // "Hi" — no truncation needed
StringTruncator.Truncate("Hello", 3); // "..." — suffix only when too short
StringTruncator| Method | Description |
|---|---|
Truncate(string text, int maxLength, string suffix = "...") | Truncate at the last word boundary before maxLength; fall back to hard truncation if no space is found |
Notes:
text fits within maxLength, it is returned unchanged.maxLength characters."...".dotnet build src/Philiprehberger.SmartTruncate.csproj --configuration Release
If you find this project useful: