c# - 将 URL 转换为 C# 字符串中的超链接的最简单方法?

标签 c# regex string hyperlink

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

您想到的最有效的方法是什么?

来自

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>

最佳答案

正则表达式可能是您完成此类任务的 friend :

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

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

关于c# - 将 URL 转换为 C# 字符串中的超链接的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32637/

相关文章:

java - Java中不带空格的字符串比较

C++:使用 std::string 在内存中移动 jpeg 是否安全?

c# - string.ToLower() 和 string.ToLowerInvariant()

c# - 在另一个列表中添加列表副本的最佳方法是什么?

c# - 找不到 System.Windows.Point

java - 替换 html 标签

ruby 正则表达式挂起

c# - 查找图像内容并在其周围绘制矩形

javascript - session 变量默认为最后一组

regex - 在字符串中的任何位置以任何顺序匹配多个模式