Skip to content

Instantly share code, notes, and snippets.

@thiagoaag
Last active October 21, 2015 17:49
Show Gist options
  • Save thiagoaag/d258c3f5dd352c3f7ccc to your computer and use it in GitHub Desktop.
Save thiagoaag/d258c3f5dd352c3f7ccc to your computer and use it in GitHub Desktop.
Truncate a long string
public static string TruncateAtWord(this string input, int length)
{
if (input == null || input.Length < length)
return input;
int iNextSpace = input.LastIndexOf(" ", length);
return string.Format("{0}...", input.Substring(0, (iNextSpace > 0) ? iNextSpace : length).Trim());
}
http://stackoverflow.com/a/1614090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment