Allow strings to be truncated
public static class TruncateExtensionsInheritance System.Object → TruncateExtensions
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);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.
System.String
The truncated string if its length exceeds length, otherwise the original string.
Returns null if input is null.
"This is a long string".Truncate(10) => "This is a…"
"Short".Truncate(10) => "Short"
null.Truncate(10) => nullThe default truncation indicator is "…" (ellipsis), and truncation occurs from the right side of the string.
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);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.
System.String
The truncated string if its length exceeds length, otherwise the original string.
Returns null if input is null.
System.ArgumentNullException
Thrown when truncator is null.
"This is a long string".Truncate(10, Truncator.FixedLength, TruncateFrom.Left) => "…ng string"
"This is a long string".Truncate(10, Truncator.FixedNumberOfWords) => "This is…"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);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.
System.String
The truncated string if its length exceeds length, otherwise the original string.
Returns null if input is null.
System.ArgumentNullException
Thrown when truncator is null.
"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"This is the most flexible truncation method, allowing full customization of the truncation behavior.
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);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.
System.String
The truncated string if its length exceeds length, otherwise the original string.
Returns null if input is null.
"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"