C# 如何更改C中字符串元素的位置#

C# 如何更改C中字符串元素的位置#,c#,C#,如何更改字符串元素的位置 例如: ... <div class="bb" id="aaa"> .... 。。。 .... 到 。。。 ... 或者 。。。 .... 到 。。。 ... 所以我需要将C中的class=”“id=”“更改为id=”“class=“。如何做?我无法想象为什么需要这样做,但您可以使用一点简单的XSLT: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr

如何更改字符串元素的位置

例如:

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

。。。
...
或者

。。。
....

。。。
...

所以我需要将C中的
class=”“id=”“
更改为
id=”“class=“
。如何做?

我无法想象为什么需要这样做,但您可以使用一点简单的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@id" />
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

问题没有很好地定义,但是如果目标是简单地交换div标记中的'id'和'class'属性,那么类似的方法就可以了:

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;
    }
 }

如果开始和结束标记不是静态字符串,您也可以替换“class”和“id”属性。我得到的印象是,有更多的细节,然后你告诉我们,所以很难找出你真正需要什么。

我敢问-你真的想通过这样做来实现什么?将
class
始终是
div
标记中的最后一个声明吗?元素中的属性位置对于HTML和XML。你为什么要控制他们的位置?@senzacionale:住手。你被骗了。@M.R:事实上,我很确定这说明了为什么正则表达式远不是最好的选择。thx。是否可以自动检测开始和结束。不是静态的结束它当然可以检测开始和结束,但必须知道问题的边界。另一个解决方案可能是将有趣的属性“就地”替换。见我的编辑上面。
...
    <div id="aaa" style="kk" class="bb">
...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@id" />
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
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;
    }
 }
    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;
    }