Skip to content

Instantly share code, notes, and snippets.

@Larry57
Created October 18, 2012 06:26
Show Gist options
  • Save Larry57/3910170 to your computer and use it in GitHub Desktop.
Save Larry57/3910170 to your computer and use it in GitHub Desktop.
Truncate a string too long for display. So, "A too long string" become "A too long s..." if limit is 12 chars.
public static string Truncate(this string myString, int limit, string symbol)
{
if (myString == null)
return null;
if (limit < 0)
throw new ArgumentOutOfRangeException("limit", limit, "must be 0 or greater");
if (symbol == null)
throw new ArgumentNullException("symbol must not be null");
if (myString.Length < limit)
return myString;
return myString.Substring(0, limit) + symbol;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment