Truncate my strings with a “…” if they are too long?

Here is the logic wrapped up in an extension method:

public static string Truncate(string value, int maxChars)
{
    return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}
Usage:

var s = "abcdefg";

Console.WriteLine(s.Truncate(3));
 

Author Profile

PREM KUMAR
PREM KUMAR
Tech Lover , Open Src Dev , Dotnet Core, Ionic, Xamarin, Node, Angular2, Python, Opencv, AI, ML, Robotics, SignalR, Iot, Love Music, Cooking, Girls, Innovation.
Like
Like Love Haha Wow Sad Angry
1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.