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

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

问题描述

我正在使用 Twitter API 并希望将所有 URL 转换为超链接.

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?

来自

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

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>");

匹配 URL 的正则表达式可能需要一些工作.

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

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

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