Share via

string replace in c#

nellie 126 Reputation points
Feb 10, 2021, 10:24 PM

Hi I have a long string (reading of a html) and I want it to search for string <a href="http://web/sub/index.aspx?articleid=9999">
where 9999 can be any number which can be also 5 digits also eg 1234 or 12345 and I want it to string replace to be <a href="9999.html"

How can I do this?
thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,147 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Abdulhakim M. Elrhumi 356 Reputation points
    Feb 11, 2021, 12:08 AM

    Hi

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ConsoleApp
    {
      class Program
      {
        static void Main(string[] args)
        {
          string mycompany = "IBM";
          Console.WriteLine(mycompany);
          Console.WriteLine(mycompany.Replace(mycompany, "Microsoft"));
          Console.ReadKey();
        }
      }
    }
    

    Best Regards.
    --please don't forget to Accept as answer if the reply is helpful--


  2. Karen Payne MVP 35,461 Reputation points
    Feb 11, 2021, 1:37 AM

    See if this will work for you.

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text.RegularExpressions;  
    using System.Web;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                var htmlCode = "<a href=\"http://web/sub/index.aspx?articleid=9999\">";  
      
                DumpHRefs(htmlCode);  
                Console.ReadLine();  
            }  
            static void DumpHRefs(string inputString)  
            {  
                var HRefPattern = @"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>\S+))";  
      
                try  
                {  
                    var match = Regex.Match(inputString, HRefPattern,   
                        RegexOptions.IgnoreCase |   
                        RegexOptions.Compiled,   
                        TimeSpan.FromSeconds(1));  
      
                    while (match.Success)  
                    {  
                        Console.WriteLine($"Found href " + match.Groups[1]);  
                        var myUri = new Uri(match.Groups[1].Value);  
                        var articleIdValue = HttpUtility.ParseQueryString(myUri.Query).Get("articleid");  
                          
                        if (!string.IsNullOrWhiteSpace(articleIdValue))  
                        {  
                            Console.WriteLine($"\t{articleIdValue}");  
                        }  
                          
                        match = match.NextMatch();  
                    }  
                }  
                catch (RegexMatchTimeoutException)  
                {  
                    Console.WriteLine("The matching operation timed out.");  
                }  
            }  
        }  
    }  
    

    66732-11111111111.png

    Edit

    Yes, note the 9999 was never searched for in the code but instead the parameters. So the parameter value returned can be concatenated e.g.

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text.RegularExpressions;  
    using System.Web;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
      
                var list = new List<string>  
                {  
                    "<a href=\"http://web/sub/index.aspx?articleid=9999\">",   
                    "<a href=\"http://web/sub/index.aspx?articleid=1\">",   
                    "<a href=\"http://web/sub/index.aspx?articleid=123\">"  
                };  
      
                foreach (var item in list)  
                {  
                    var results = DumpHRefs(item);  
                    Console.WriteLine(item);  
                    if (results.Count == 1)  
                    {  
                        Console.WriteLine($"\t{results.FirstOrDefault()}");  
                    }  
      
                }  
      
      
                Console.ReadLine();  
            }  
            static List<string> DumpHRefs(string inputString)  
            {  
                var resultList = new List<string>();  
                  
                var HRefPattern = @"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>\S+))";  
      
                try  
                {  
                    var match = Regex.Match(inputString, HRefPattern,   
                        RegexOptions.IgnoreCase |   
                        RegexOptions.Compiled,   
                        TimeSpan.FromSeconds(1));  
      
                    while (match.Success)  
                    {  
      
                        var myUri = new Uri(match.Groups[1].Value);  
                        var articleIdValue = HttpUtility.ParseQueryString(myUri.Query).Get("articleid");  
                          
                        if (!string.IsNullOrWhiteSpace(articleIdValue))  
                        {  
                            resultList.Add($"{articleIdValue}.html");  
                        }  
                          
                        match = match.NextMatch();  
                    }  
                }  
                catch (RegexMatchTimeoutException)  
                {  
                    Console.WriteLine("The matching operation timed out.");  
                }  
      
                return resultList;  
            }  
        }  
    }  
    

    67113-222222.png


  3. Daniel Zhang-MSFT 9,631 Reputation points
    Feb 11, 2021, 3:12 AM

    Hi nellie-2760
    I suggest you use regular expressions to match urls in the string.
    Here is my test code example you can refer to.

    static void Main(string[] args)  
    {  
        string longS = @"<object>  
                            <node>  
                                <a href='http://web/sub/index.aspx?articleid=9999'>  
                                <a href='http://web/sub/index.aspx?articleid=12345'>  
                            </node>  
                            </object>";  
        string regexString = @"(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";  
        string replacement = Regex.Replace(longS, regexString, "9999.html");  
        Console.WriteLine(replacement);  
        Console.ReadKey();  
    }  
    

    The result:
    66723-211.png
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.