Given the line -
var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
1. The first scenario
var maxlenght = 15; var shortString =(string .Length > maxlenght ) ? string.Substring(0,maxlenght ) + "..." : string;
Exit: "Lorem ipsum dol ...";
2. The second scenario
var shortString =(string .Length > maxlenght ) ? string.Substring(0, string.IndexOf(" ", 15)) + "..." : string;
Exit: "Lorem ipsum dolor ...";
if u wants the last word to be complete, then the best second senario ...
now it works fine ...
public static string GetShortTitleForDisplay(string title, int noOfCharacter) { var shortTitle = title; if (title.Length <= noOfCharacter) return shortTitle; var pos = title.IndexOf(" ", noOfCharacter, StringComparison.Ordinal); if (pos > 0) shortTitle = title.Substring(0, pos) + " ..."; return shortTitle; }
source share