/// <summary> /// Crop a text if too long, add in that case, also add an ellipsis &hellip; or a custom suffix (optional) /// </summary> /// <param name="value">Value to maybe truncate (using safe-truncate - see Text.Crop)</param> /// <param name="length">Max length to keep</param> /// <param name="suffix">optional suffix, defaults to &hellip;</param> /// <remarks>If you don't need the suffix, use CropText(...) instead</remarks> /// <returns></returns> public static string Ellipsis(string value, int length, string suffix = null) { var truncated = Truncator.SafeTruncate(value, length); var addExtension = truncated != value; var extension = addExtension ? (suffix ?? Defaults.HtmlEllipsisCharacter) : ""; return(truncated + extension); }
public void Truncate_DontBacktrackIfNoSpace() { var withoutSpace = simpleTruncates.Replace(" ", "x"); // remove all white-spaces Assert.AreEqual(withoutSpace.Substring(0, 15), Truncator.SafeTruncate(withoutSpace, 15), "truncate till end of word"); Assert.AreEqual(withoutSpace.Substring(0, 16), Truncator.SafeTruncate(withoutSpace, 16), "truncate till end of word"); Assert.AreEqual(withoutSpace.Substring(0, 17), Truncator.SafeTruncate(withoutSpace, 17), "truncate till end of word"); Assert.AreEqual(withoutSpace.Substring(0, 18), Truncator.SafeTruncate(withoutSpace, 18), "truncate till end of word"); }
private static void TestChars15To20(string partBefore16, string testStr) { Assert.AreNotEqual(partBefore16, Truncator.SafeTruncate(testStr, 15), "15 chars should be shorter"); for (var i = 16; i < 20; i++) { Assert.AreEqual(partBefore16, Truncator.SafeTruncate(testStr, i), $"truncate till end of word count: {i}"); } Assert.AreNotEqual(partBefore16, Truncator.SafeTruncate(testStr, 20), "20 should get next length end of word"); }
public void Truncate_CitiesUmlauts() { Assert.AreEqual("Züric", Truncator.SafeTruncate(citiesUmlauts, 5), "Zürich has 6/5 chars"); Assert.AreEqual("Zürich", Truncator.SafeTruncate(citiesUmlauts, 6), "Zürich has 6/6 chars"); Assert.AreEqual("Zürich", Truncator.SafeTruncate(citiesUmlauts, 7), "Zürich has 6/8 chars"); Assert.AreEqual("Zürich &", Truncator.SafeTruncate(citiesUmlauts, 8), "Zürich has 6/8 chars"); Assert.AreEqual("Zürich &", Truncator.SafeTruncate(citiesUmlauts, 10), "Zürich-and has 10/10 chars"); Assert.AreEqual("Zürich &", Truncator.SafeTruncate(citiesUmlauts, 11), "Zürich-and has 10/11 chars"); Assert.AreEqual("Zürich & München", Truncator.SafeTruncate(citiesUmlauts, 16), "Zürich-and has M.. 16 chars"); }
/// <summary> /// Cut off a text at the best possible place with a max-length. /// This will count html-entities like & or umlauts as 1 character, /// and will try to cut off between words if possible. /// </summary> /// <param name="value">String to cut off. Can contain umlauts and html-entities, but should not contain html-tags as there are not treated properly.</param> /// <param name="length">length to cut off at</param> /// <returns></returns> public static string Crop(string value, int length) { return(Truncator.SafeTruncate(value, length)); }
/// <summary> /// Cut off a text at the best possible place with a max-length. /// This will count html-entities like &, &nbsp; or umlauts as 1 character, /// and will try to cut off between words if possible. /// So it will backtrack to the previous space. /// </summary> /// <param name="value">String to cut off. Can contain umlauts and html-entities, but should not contain html-tags as there are not treated properly.</param> /// <param name="length">length to cut off at</param> /// <returns></returns> public static string Crop(this string value, int length) => Truncator.SafeTruncate(value, length);