Search code examples
c#asp.netstringhyperlinktrim

Trimming or formatting an asp net hyperlink


As the title says: I want to trim or format a very long hyperlink. if the text is to long, I want the code to know that and replace the remaining strings with "..." for example: "[email protected]". as soon as "account" starts, I want it to be replaced with "..."

I've tried trim, but it does not work.

C#:

var getContact = _ecSystem.GetContact(ContactId.Value);
    hlEmail.Text = getContact.Email.Trim(); //getContact.Email is a string.

ASPX:

<asp:HyperLink runat="server" ID="hlEmail" NavigateUrl="#"  />

Solution

  • Have you tried using the regular SubString method?

    var emailaddress = getContact.Email.Trim();
    hlEmail.Text = emailaddress.Length > 20 ? emailaddress.SubString(0, 17) + "..." : emailaddress;
    

    But as @RahulSingh said the most elegant way to do this is by using css and the text-overflow property, then limit the size of the html container the link is within.

    <a style="text-overflow: ellipsis; width: 50px; float: left; overflow: hidden;" href="mailto:[email protected]">[email protected]</a>