How to convert a URL inside a string to a hyperlink in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string str = "This is my website check it out https://www.collectivesolver.com";
        
        Regex r = new Regex(@"(https?://[^\s]+)");
        str = r.Replace(str, "<a href=\"$1\">$1</a>");
        
        Console.WriteLine(str);
    }
}



/*
run:

This is my website check it out <a href="https://www.collectivesolver.com">https://www.collectivesolver.com</a>

*/

 



answered May 1, 2025 by avibootz
...