How to show an ellipses (...) at the end of the text in a textarea?

c#, textbox, windows-phone, windows-phone-8

Solution

Given the desired text string and the maximum characters length of your text-box, use this extension method to solve it:

public static string TruncateAtWord(this string input, int length)
{
    if (input == null || input.Length < length)
        return input;

    int iNextSpace = input.LastIndexOf(" ", length);

    return string.Format("{0}...", input.Substring(0, (iNextSpace > 0) ? iNextSpace : length).Trim());
}

Usage:

var ellipsisedString = "this is a very long string and I want to cut it with ellipsis!".TruncateAtWord(25);

Result:

"this is a very long..."

Problem

How i can add an ellipses (...) to end of the text in a textbox if there is no space to show the remain text or sentence in WP8 using C#?

Original source