locked
C# string replace Custom RRS feed

  • Question

  • User-1118822443 posted

    string data = "Hello World!.How are<br> you?";
    string searchStr = "are you?";
    string result = data.replace(searchStr,""good")

    output should be ------>Hello World!.How good<br>

    I want to search searchStr in data variable and replace the content. Basically i want to ignore<br> in string search. Is it possible?

    Wednesday, August 8, 2018 2:22 PM

All replies

  • User-492460945 posted

    Hi Mahesh,

    You can do something like below.

    string finalStr = data.Replace("<br>", "").Replace(searchStr, "I am good");

    Wednesday, August 8, 2018 2:39 PM
  • User283571144 posted

    Hi Mahesh420,

    According to your description, we should firstly remove the br then searching.

    Then you should write some logic to add the br again.

    More details, you could refer to below codes:

            static void Main(string[] args)
            {
                string resultwithbr;
                string data = "Hello World!.How are<br> you?";
                int indexofbr = data.IndexOf("<br>");
                string searchStr = "are you?";
                string result = data.Replace("<br>","").Replace(searchStr, "good");
                int indexofgood = result.IndexOf("good");
                if (indexofgood > indexofbr -4)
                {
                    resultwithbr = result.Insert(indexofgood+4, "<br>");
                }
                else
                {
                    resultwithbr = result.Insert(indexofgood, "<br>");
                }
                Console.Write(resultwithbr);
                Console.ReadLine();
                          
            }

    Result:

    Best Regards,

    Brando

    Thursday, August 9, 2018 2:52 AM