skip to Main Content

I want to manipulate the following HTML structure using C#, and move the bgcolor attribute inside the style attribute as shown below, I want to achieve it using string manipulation (Or any other suitable method if applicable) is there a way possible:

Present Structure

<body>
    <div bgcolor="#342516" style="color: red; font-size:10px;">ABCD</div>
    <div bgcolor="#342516" style="color: red; font-size:10px;">EFGH</div>
    <div bgcolor="#342516" style="color: red; font-size:10px;">HIJK</div>
    <div bgcolor="#342516" style="color: red; font-size:10px;">LMNO</div>
</body>

Required Output

<body>
    <div style="background-color:#342516; color: red; font-size:10px;">ABCD</div>
    <div style="background-color:#342516; color: red; font-size:10px;">EFGH</div>
    <div style="background-color:#342516; color: red; font-size:10px;">HIJK</div>
    <div style="background-color:#342516; color: red; font-size:10px;">LMNO</div>
</body>

2

Answers


  1. I don’t think you need to program to solve this, a text editor will suffice. As far as I know, both VS Code and NotePad++ support replacing text by folder.

    Replace bgcolor="#342516" style=" with style="background-color:#342516;

    Login or Signup to reply.
  2. If You really need it in C# the most simple solution is string.replace(). for any other more complex text You need regular expression

        var oldString = "<body>rn    <div bgcolor="#342516" style="color: red; font-size:10px;">ABCD</div>rn    <div bgcolor="#342516" style="color: red; font-size:10px;">EFGH</div>rn    <div bgcolor="#342516" style="color: red; font-size:10px;">HIJK</div>rn    <div bgcolor="#342516" style="color: red; font-size:10px;">LMNO</div>rn</body>";
        var newString = oldString.Replace("bgcolor="#342516" style="", "style="background-color:#342516; ");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search