Created
May 2, 2024 16:23
-
-
Save wchesley/f64c7e15763366fc6379bb1cf54fa007 to your computer and use it in GitHub Desktop.
C# Truncate string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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