Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6338005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:21:36+00:00 2026-05-24T19:21:36+00:00

How to change string element position. Example: … <div class=bb id=aaa> …. to …

  • 0

How to change string element position.

Example:

...
<div class="bb" id="aaa">
....

to

...
    <div id="aaa" class="bb">
...

or.

...
    <div class="bb" style="kk" id="aaa">
    ....

to

...
    <div id="aaa" style="kk" class="bb">
...

So i need to change class="" id="" to id="" class="" in C#. How?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:21:37+00:00Added an answer on May 24, 2026 at 7:21 pm

    The problem isn’t well defined but if the goal is to simply swap the ‘id’ and ‘class’ properties in a div tag then something like this would work:

    public class SSwap
    {
        string str1 = @"<div class=""bb"" id=""aaa"">";
        string str2 = @"<div class=""bb"" style=""kk"" id=""aaa"">";
    
        public SSwap()
        {
            Console.WriteLine("Before : " + str1 + "\nAfter : " + swap_string(str1));
            Console.WriteLine("Before : " + str2 + "\nAfter : " + swap_string(str2));
        }
    
        public string swap_string(string str)
        {
            string retStr = "<div ";
            Regex theRegex = new Regex(@"(\w+)=(""\w+"")");
            Dictionary<string, string> props = new Dictionary<string, string>();
            Match m = theRegex.Match(str);
            int classPos=-1, idPos=-1, pos = 0;
    
            while (m.Success)
            {
                if (m.Result("$1") == "class") classPos = pos; // Remember where class was
                if (m.Result("$1") == "id") idPos = pos; // Remember where id was
                props[m.Result("$1")] = m.Result("$2");
                m = m.NextMatch();
                pos++;
            }
            pos = 0;
            foreach (string s in props.Keys)
            {
                if (pos == classPos)  // put id where class was
                {
                    retStr += @"id=" + props["id"] + @" ";
                }
                else if (pos == idPos) // put class where id was
                {
                    retStr += @"class=" + props["class"] + @" ";
                }
                else // put everything else where ever it appears in the dictionary
                {
                    retStr += s + @"=" + props[s] + @" ";
                }
                pos++;
            }
            retStr += ">";
            return retStr;
        }
     }
    

    Of course this has no error checking, doesn’t react well to strings not strictly formatted like the two examples you gave, is not tolerant of mixed case, etc. Some clever regular expression manipulation could probably accomplish it in a couple lines but it’s unclear that complex, unreadable, hard to maintain regex is what you seek.

        public string swap_string(string str)
        {
            Regex classRegex = new Regex(@"class=""(.+?)""");
            Regex idRegex = new Regex(@"id=""(.+?)""");
            string classVal = classRegex.Match(str).Value;
            string idVal = idRegex.Match(str).Value;
            str = classRegex.Replace(str, "__TMPSTRING__");
            str = idRegex.Replace(str, classVal);
            str = str.Replace("__TMPSTRING__", idVal);
            return str;
        }
    

    If the start and end tags are not static strings, you can also just replace the ‘class’ and ‘id’ properties in-place. I get the impression that there are more details then you’re telling us so it’s quite hard to figure out what you really need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My php is weak and I'm trying to change this string: http://www.example.com/backend.php?/c=crud&m=index&t=care ^ to
You can change the connection string at run-time like this. You make the connection
Is it possible to change the connection string of a bound dataset during runtime?
How can I change values in string from 0,00 to 0.00? - only numeric
How can I change the connection string from nhibernate.config file using nant the problem
Is there a way to change the connection string of a DataBase object in
Is it possible to change a database connection string value in the Web.Config at
Is there a way to change the encoding used by the String(byte[]) constructor ?
string = 'Hello _1234_ world _4567_'; string.match(/(\d*)/gi); //,,,,,,,1234,,,,,,,,,,4567,, If I change regext to have
I'd like to set a connection string programmatically, with absolutely no change to any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.