Diese Methode kürzt einen String auf eine beliebige Länge ohne das, das letzte Wort abgeschnitten wird.
/// <summary>
/// Cuts the long string.
/// </summary>
/// <param name="longString">The long string.</param>
/// <param name="length">The length.</param>
/// <returns></returns>
private string CutLongString(string longString, int length)
{
if (longString.Trim() != string.Empty)
{
if (longString.Length > length)
{
longString = longString.Substring(0, length);
int positionLastSpace = longString.LastIndexOf(" ");
if (positionLastSpace > -1 && positionLastSpace < length)
longString = longString.Substring(0, positionLastSpace);
longString += " …";
}
}
return longString;
}
1 Kommentare zum Snippet