C# method to truncate a text by keeping words.
public static String TruncateWithPreservation(String s, int len) { String[] words = Regex.Split(s, "\\s "); String finalResult = ""; foreach (String word in words) { String tmp = finalResult " " word; if (tmp.Length > len) { return finalResult " ..."; } finalResult = tmp; } return finalResult; }
Comments