Skip to content

Instantly share code, notes, and snippets.

@wchesley
Created May 2, 2024 16:23
Show Gist options
  • Save wchesley/f64c7e15763366fc6379bb1cf54fa007 to your computer and use it in GitHub Desktop.
Save wchesley/f64c7e15763366fc6379bb1cf54fa007 to your computer and use it in GitHub Desktop.
C# Truncate string
public static class TruncateString
{
/// <summary>
/// Truncate a string to a specified length
/// </summary>
/// <param name="value">String to truncate</param>
/// <param name="maxLength">Maximum length of string</param>
/// <returns>Truncated string value.</returns>
public static string? Truncate(this string? value, int maxLength)
{
return value?.Length > maxLength
? value.Substring(0, maxLength)
: value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment