Humanizer.TruncateExtensions
TruncateExtensions Class
Allow strings to be truncated
public static class TruncateExtensions
Inheritance System.Object → TruncateExtensions
Methods
TruncateExtensions.Truncate(this string, int) Method
Truncates a string to a specified maximum length using the default truncation string ("…") and fixed-length truncator.
public static string? Truncate(this string? input, int length);
Parameters
input System.String
The string to be truncated. Can be null.
length System.Int32
The maximum length of the result string, including the truncation indicator.
Returns
System.String
The truncated string if its length exceeds length, otherwise the original string.
Returns null if input is null.
Example
"This is a long string".Truncate(10) => "This is a…"
"Short".Truncate(10) => "Short"
null.Truncate(10) => null
Remarks
The default truncation indicator is "…" (ellipsis), and truncation occurs from the right side of the string.
TruncateExtensions.Truncate(this string, int, ITruncator, TruncateFrom) Method
Truncates a string to a specified maximum length using a custom truncator and truncation direction.
public static string? Truncate(this string? input, int length, Humanizer.ITruncator truncator, Humanizer.TruncateFrom from=Humanizer.TruncateFrom.Right);
Parameters
input System.String
The string to be truncated. Can be null.
length System.Int32
The maximum length of the result string, including the truncation indicator.
truncator ITruncator
The ITruncator implementation to use for truncation logic. Must not be null.
from TruncateFrom
Specifies from which side of the string to truncate. Default is Right.
Returns
System.String
The truncated string if its length exceeds length, otherwise the original string.
Returns null if input is null.
Exceptions
System.ArgumentNullException
Thrown when truncator is null.
Example
"This is a long string".Truncate(10, Truncator.FixedLength, TruncateFrom.Left) => "…ng string"
"This is a long string".Truncate(10, Truncator.FixedNumberOfWords) => "This is…"
TruncateExtensions.Truncate(this string, int, string, ITruncator, TruncateFrom) Method
Truncates a string to a specified maximum length using a custom truncation string, truncator, and direction.
public static string? Truncate(this string? input, int length, string? truncationString, Humanizer.ITruncator truncator, Humanizer.TruncateFrom from=Humanizer.TruncateFrom.Right);
Parameters
input System.String
The string to be truncated. Can be null.
length System.Int32
The maximum length of the result string, including the truncation indicator.
truncationString System.String
The string to use as the truncation indicator (e.g., "...", "…", or any custom string). Can be null or empty.
truncator ITruncator
The ITruncator implementation to use for truncation logic. Must not be null.
from TruncateFrom
Specifies from which side of the string to truncate. Default is Right.
Returns
System.String
The truncated string if its length exceeds length, otherwise the original string.
Returns null if input is null.
Exceptions
System.ArgumentNullException
Thrown when truncator is null.
Example
"This is a long string".Truncate(10, "...", Truncator.FixedLength, TruncateFrom.Right) => "This is..."
"This is a long string".Truncate(10, "…", Truncator.FixedNumberOfWords, TruncateFrom.Left) => "… string"
Remarks
This is the most flexible truncation method, allowing full customization of the truncation behavior.
TruncateExtensions.Truncate(this string, int, string, TruncateFrom) Method
Truncates a string to a specified maximum length using a custom truncation string and fixed-length truncator.
public static string? Truncate(this string? input, int length, string? truncationString, Humanizer.TruncateFrom from=Humanizer.TruncateFrom.Right);
Parameters
input System.String
The string to be truncated. Can be null.
length System.Int32
The maximum length of the result string, including the truncation indicator.
truncationString System.String
The string to use as the truncation indicator (e.g., "...", "…", or any custom string). Can be null or empty.
from TruncateFrom
Specifies from which side of the string to truncate. Default is Right.
Returns
System.String
The truncated string if its length exceeds length, otherwise the original string.
Returns null if input is null.
Example
"This is a long string".Truncate(10, "...") => "This is..."
"This is a long string".Truncate(15, "--") => "This is a lo--"
"This is a long string".Truncate(10, "...", TruncateFrom.Left) => "...string"