Skip to content

Instantly share code, notes, and snippets.

@teamday
Last active November 2, 2017 20:08
Show Gist options
  • Save teamday/c29d72e964264ea8654f4a713ebc66c5 to your computer and use it in GitHub Desktop.
Save teamday/c29d72e964264ea8654f4a713ebc66c5 to your computer and use it in GitHub Desktop.
Trim last wrod regexp "Blah Blah Blah Bl..." -> "Blah Blah Blah..."
using System.Text.RegularExpressions;
public static class TrimLastWord{
private static Regex _trimLastWord = new Regex(@"\s+?(\S+)?$");
private static string TrimLastWord(this string str, int maxLength, string extra="...")
{
if (str?.Length > maxLength)
return $"{_trimLastWord.Replace(str.Substring(0, maxLength), "")}{extra}";
return str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment