Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Last active December 10, 2023 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonsmits/e6f9a9f28c1ab76711dea1c1eb375009 to your computer and use it in GitHub Desktop.
Save ramonsmits/e6f9a9f28c1ab76711dea1c1eb375009 to your computer and use it in GitHub Desktop.
string.Truncate extension method that if longer than the specific maximum truncates the string and adds `…` character (tree dots)
static class StringExt
{
public static string? Truncate(this string? value, int maxLength)
{
if (value == null) return value;
return value.Length <= maxLength ? value : string.Concat(value.AsSpan(0, maxLength - 1), "…");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment