加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将字符串转换为html(超链接)

发布时间:2020-12-16 01:49:43 所属栏目:百科 来源:网络整理
导读:我为字符串类定义了To Html()扩展,并将break转换为 br /. 如何检测超链接并转换为 a元件? public static class StringExtension{ public static string ToHtml(this string item) { //rn windows //n unix //r mac os return item.Replace("rn","br/"
我为字符串类定义了To Html()扩展,并将break转换为< br />.
如何检测超链接并转换为< a>元件?

public static class StringExtension
{
    public static string ToHtml(this string item)
    {
        //rn windows
        //n unix
        //r mac os
        return item.Replace("rn","<br/>").Replace("n","<br/>").Replace("r","<br/>");
    }
}

c#,asp.net

解决方法

see this one,使用正则表达式

private string ConvertUrlsToLinks(string msg) {
    string regex = @"((www.|(http|https|ftp|news|file)+://)[&#95;.a-z0-9-]+.[a-z0-9/&#95;:@=.+?,##%&~-]*[^.|'|# |!|(|?|,| |>|<|;|)])";
    Regex r = new Regex(regex,RegexOptions.IgnoreCase);
    return r.Replace(msg,"<a href="$1" title="Click to open in a new window or tab" target="&#95;blank">$1</a>").Replace("href="www","href="http://www");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读