Language: C#
TextAbstract Method
/// <summary> /// Returns an abstract of the provided text by returning up to Length characters /// of a text string. If the text is truncated a ... is appended. /// </summary> /// <param name="Text">Text to abstract</param> /// <param name="Length">Number of characters to abstract to</param> /// <returns>string</returns> public static string TextAbstract(string Text, int Length) { if (Text.Length <= Length) return Text; Text = Text.Substring(0, Length - 2); Text = Text.Substring(0, Text.LastIndexOf(" ")); return Text + "..."; }
Tags:
Comment:
A simple function to truncate a string on a word boundary with an ellipsis if text exceeds a specified length.
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search


If the initial substring returns a text ending in a space or a space followed by a single letter such as 'I' or 'a', then your actual length after adding the three character ellipse will be more than the requested length.