It is best to create an extension method for simple string manipulations
Option 1 - using substring:
public static string Truncate( this string myString, int maxLength )
{
if (string.IsNullOrEmpty(myString)) { return myString; }
return myString.Substring(0, Math.Min(myString.Length, maxLength));
}
Option 2 - using linq:
myString = string.Concat(myString.Take(maxLength));
Option 3 - using index operator (c# 8):
myString = myString[..Math.Min(30, myString.Length)];
Tags
C#