到URL转换为超链接在C#字符串最简单的方法? [英] Easiest way to convert a URL to a hyperlink in a C# string?

查看:926
本文介绍了到URL转换为超链接在C#字符串最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我消耗了Twitter的API,并希望所有的URL转换为超链接。

什么是你想出来的最有效的方式做到这一点?

 字符串的myString =这是我的鸣叫检查出来http://tinyurl.com/blah

 这是我的鸣叫检查出来< A HREF =htt​​p://tinyurl.com/blah> HTTP://tinyurl.com/>等等< / A>


解决方案

正前pressions可能是你的朋友对这样的任务:

 正则表达式R =新的正则表达式(@(HTTPS:// [^ \\ S] +));
的myString = r.Replace(myString的,&所述; A HREF = \\$ 1 \\> $ 1所述; / A>中);

常规前pression匹配的网址,可能需要做一些工作。

I am consuming the Twitter API and want to convert all URLs to hyperlinks.

What is the most effective way you've come up with to do this?

from

string myString = "This is my tweet check it out http://tinyurl.com/blah";

to

This is my tweet check it out <a href="http://tinyurl.com/blah">http://tinyurl.com/>blah</a>

解决方案

Regular expressions are probably your friend for this kind of task:

Regex r = new Regex(@"(https?://[^\s]+)");
myString = r.Replace(myString, "<a href=\"$1\">$1</a>");

The regular expression for matching URLs might need a bit of work.

这篇关于到URL转换为超链接在C#字符串最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆