Truncate
Truncate the specified string based on the given length. Replaces the truncated data to "..."
Source
public static class StringEx
{
/// <summary>
/// Truncates the string to a specified length and replace the truncated to a ...
/// </summary>
/// <param name="text">string that will be truncated</param>
/// <param name="maxLength">total length of characters to maintain before the truncate happens</param>
/// <returns>truncated string</returns>
public static string Truncate(this string text, int maxLength)
{
// replaces the truncated string to a ...
const string suffix = "...";
string truncatedString = text;
if (maxLength <= 0) return truncatedString;
int strLength = maxLength - suffix.Length;
if (strLength <= 0) return truncatedString;
if (text == null || text.Length <= maxLength) return truncatedString;
truncatedString = text.Substring(0, strLength);
truncatedString = truncatedString.TrimEnd();
truncatedString += suffix;
return truncatedString;
}
}
Example
string newText = "this is the palce i want to be, Cindys is the place to be!";
Console.WriteLine("New Text: {0}", newText.Truncate(40));