Skip to content

Instantly share code, notes, and snippets.

@ArseniySavin
Last active August 23, 2016 08:33
Show Gist options
  • Save ArseniySavin/bea25442e1a490853c1111cc10e6297d to your computer and use it in GitHub Desktop.
Save ArseniySavin/bea25442e1a490853c1111cc10e6297d to your computer and use it in GitHub Desktop.
Truncation string
public static string TruncateString(this string value, int maxLength)
{
int valueLength = value.Length;
const string CHARACTER_REPLACE = "...";
int startPosition = (maxLength / 3);
if(valueLength > maxLength)
{
try
{
int modificationLength = ((valueLength - maxLength) + CHARACTER_REPLACE.Length);
value = value.Substring(startPosition, modificationLength);
}
catch(Exception ex)
{
throw ex;
}
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment