/// <summary>
/// Truncate string at a word near to the limit specified. Avoid word split.
/// </summary>
/// <param name="input">string</param>
/// <param name="length"></param>
/// <param name="appendDots"></param>
/// <returns></returns>
public static string TruncateAtWord(this string input, int length, bool appendDots = false)
{
if (input == null || input.Length < length)
return input;
var iNextSpace = input.LastIndexOf(" ", length, StringComparison.Ordinal);
var trimmedInput = string.Format("{0}", input.Substring(0, (iNextSpace > 0) ? iNextSpace : length).Trim());
if (appendDots)
{
return trimmedInput + "...";
}
return trimmedInput;
}
Tests
[Fact]
public void TruncateAtWordTests()
{
"This is a long sentence".TruncateAtWord(6).Should().Be("This");
"This is a long sentence".TruncateAtWord(7).Should().Be("This is");
}