The ASP.NET C# code below will automatically detect if your string content contains url[s] or email address[es] then wrap them into hyperlink[s] or mailto email address[es] respectively.
The example below makes a URL and an email address click-able. If you click on the URL, it will open the URL in a new web page and if you click on the email address, it will open a default email composer which is config in your current browser.
The ASP.NET C# source code
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text.RegularExpressions; public partial class convert_text_to_url_and_mailto : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string content = "Welcome to http://4rapiddev.com" + ", a website for developers, designers and webmaster.<br>"; content += "If you have any feedback, question or contribution please send an email to [email protected]"; Regex url_regex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled); Regex email_regex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled); content = url_regex.Replace(content, "<a href=\"$1\" target=\"_blank\">$1</a>"); content = email_regex.Replace(content, "<a href=mailto:$1>$1</a>"); Response.Write(content); } } |
The HTML Output
Welcome to <a href="http://4rapiddev.com," target="_blank">http://4rapiddev.com,</a> a website for developers, designers and webmaster.<br>If you have any feedback, question or contribution please send an email to <a href=mailto:[email protected]>[email protected]</a> |
Note: it’s required to use the Namespace: System.Text.RegularExpressions