Here is the logic wrapped up in an extension method:
public static string Truncate(this string value, int maxChars)
{
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}
Usage:
var s = "abcdefg";
Console.WriteLine(s.Truncate(3));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…