Belal Ghanem
Belal Ghanem

Reputation: 107

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

enter image description here

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#?

Upvotes: 3

Views: 4465

Answers (3)

David Gordon
David Gordon

Reputation: 554

As an alternative to the other two solutions, you could use TextTrimming="WordEllipsis" in the XAML, it just depends on whether you want to limit the string to a certain number of characaters or let the trimming occur based on textbox size.

Upvotes: 6

André C. Andersen
André C. Andersen

Reputation: 9375

I used the following code to truncate strings when I needed some simple text logging. Might be an inspiration.

public static string Truncate(this string yourString, int maxLength)
{
    return yourString.Substring(0, Math.Min(maxLength, yourString.Length));
}

Reviewing Yair Nevet's answer I find his take on the problem more complete. You can use the code above similarly to his answer:

string yourString = "Your long text goes here".Truncate(10);

Upvotes: 1

Yair Nevet
Yair Nevet

Reputation: 13003

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..."

Upvotes: 5

Related Questions