正如标题所说:我想修剪或格式化一个很长的超链接。如果文本太长,我希望代码知道这一点并将剩余的字符串替换为“...”,例如:“averylongemailaccountexample@example.com”。 “帐户”一开始,我希望将其替换为“...”
我试过修剪,但它不起作用。
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="#" />
最佳答案
您是否尝试过使用常规 SubString method ?
var emailaddress = getContact.Email.Trim();
hlEmail.Text = emailaddress.Length > 20 ? emailaddress.SubString(0, 17) + "..." : emailaddress;
但正如@RahulSingh 所说,最优雅的方法是使用 css and the text-overflow property ,然后限制链接所在的 html 容器的大小。
<a style="text-overflow: ellipsis; width: 50px; float: left; overflow: hidden;" href="mailto:averylongemailaccountexample@example.com">averylongemailaccountexample@example.com</a>
关于c# - 修剪或格式化 asp 网络超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42245400/