docs/string-truncation.md
Humanizer provides intelligent string truncation with multiple strategies to handle different use cases.
Truncation is useful when you need to limit string length for display purposes while maintaining readability. Humanizer offers several truncation strategies and uses the … character (one character) instead of "..." (three characters) to maximize visible text.
using Humanizer;
"Long text to truncate".Truncate(10)
// => "Long text…"
"Long text to truncate".Truncate(10, "---")
// => "Long te---"
Truncates to a specific total length including the truncation indicator:
"Long text to truncate".Truncate(10, Truncator.FixedLength)
// => "Long text…"
"Long text to truncate".Truncate(10, "---", Truncator.FixedLength)
// => "Long te---"
Truncates to a specific number of alphanumeric characters:
"Long text to truncate".Truncate(6, Truncator.FixedNumberOfCharacters)
// => "Long t…"
"Long text to truncate".Truncate(6, "---", Truncator.FixedNumberOfCharacters)
// => "Lon---"
Truncates to a specific number of words:
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords)
// => "Long text…"
"Long text to truncate".Truncate(2, "---", Truncator.FixedNumberOfWords)
// => "Long text---"
Similar to fixed length but attempts to preserve whole words:
"Long text to truncate".Truncate(10, Truncator.DynamicLengthAndPreserveWords)
// => "Long text…"
"Long text to truncate".Truncate(10, "---", Truncator.DynamicLengthAndPreserveWords)
// => "Long---"
Similar to fixed number of characters but attempts to preserve whole words:
"Long text to truncate".Truncate(6, Truncator.DynamicNumberOfCharactersAndPreserveWords)
// => "Long…"
"Long text to truncate".Truncate(6, "---", Truncator.DynamicNumberOfCharactersAndPreserveWords)
// => "---"
By default, truncation happens from the right (end) of the string. You can truncate from the left (beginning) using TruncateFrom.Left:
"Long text to truncate".Truncate(10, Truncator.FixedLength, TruncateFrom.Left)
// => "… truncate"
"Long text to truncate".Truncate(10, "---", Truncator.FixedLength, TruncateFrom.Left)
// => "---runcate"
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords, TruncateFrom.Left)
// => "…to truncate"
You can use any string as a truncation indicator:
"Long text to truncate".Truncate(15, " [more]")
// => "Long [more]"
"Long text to truncate".Truncate(15, "...")
// => "Long text..."
Implement the ITruncator interface to create custom truncation logic:
public interface ITruncator
{
string Truncate(string value, int length, string truncationString, TruncateFrom truncateFrom = TruncateFrom.Right);
}
Example custom truncator:
public class SentenceTruncator : ITruncator
{
public string Truncate(string value, int length, string truncationString, TruncateFrom truncateFrom)
{
// Custom logic to truncate at sentence boundaries
// Implementation details...
}
}
// Usage
"First sentence. Second sentence. Third sentence."
.Truncate(30, new SentenceTruncator());
var article = "This is a very long article that needs to be truncated for the preview.";
var preview = article.Truncate(50, Truncator.FixedNumberOfWords);
// => "This is a very long article that needs to be…"
var fileName = "Very_Long_File_Name_That_Needs_Truncation.pdf";
var displayName = fileName.Truncate(20, "…", Truncator.DynamicLengthAndPreserveWords);
// => "Very_Long_File_Name…"
var comment = "This is a user comment that might be too long to display in a notification.";
var notification = comment.Truncate(40);
// => "This is a user comment that might be…"
Choose the right strategy:
FixedLength for consistent visual lengthFixedNumberOfWords when word boundaries matterConsider the truncation indicator:
… (single character) for space efficiency"..." if your audience expects it" [more]" or similar for clarityTest with your actual content: Different text may truncate differently depending on word boundaries