【问题标题】:How to replace the last occurence of the character in .net如何替换.net中最后出现的字符
【发布时间】:2011-11-03 18:43:58
【问题描述】:

我想替换最后出现的<li> 以在其中添加类,它应该如下所示。 <li class="last">

<li><span>Vislink Acquires Gigawave for US$6 Million </span></li>
<li><span>Newegg Offers $25 7-inch DTV</span></li>
<li><span>The Hope of Broadcasters  </span></li>
<li><span>ICANN Approves Custom Domain Extensions  </span></li>
<li><span>Sciences Ending U.S. Sales  </span></li>
<li><span>LightSquared Proposes Plan to  </span></li>
<li><span>Wimbledon Gets 3D upgrade </span></li>
<li><span>The Hope of Broadcasters  </span></li>
<li><span>LightSquared Proposes Plan to  </span></li>
<li class="last"><span> Newegg Offers $25 7-inch DTV  </span></li>

我已将上述 html 存储在一个字符串变量中。 我该怎么做才能做到这一点。

【问题讨论】:

    标签: .net string replace last-occurrence


    【解决方案1】:

    如果您在代码后面执行此操作,我认为是这种情况,请尝试以下操作:

        var last = htmlString.LastIndexOf("<li>");
        htmlString = htmlString.Remove(last, 4).Insert(last, "<li class=\"last\"");
    

    【讨论】:

      【解决方案2】:
      string data = ...;
      string toReplace ="<li>";
      string toPlace = "<li class=\"last\">";
      int idx = data.LastIndexOf(toReplace);
      data = data.Remove(idx, toReplace.Length).Insert(idx, toPlace);
      

      【讨论】:

        【解决方案3】:

        也许您可以使用 jquery 来实现:

        $("li:last").addClass("last")
        

        【讨论】:

        • 实际上我是从数据库中获取html格式的记录,所以在页面上显示时,我需要添加那个类,所以我需要使用.net
        【解决方案4】:

        你可以在 jQuery 中这样做:

        $("#listid li:last").addClass("last");
        

        【讨论】:

          猜你喜欢
          • 2023-03-18
          • 2011-04-19
          • 1970-01-01
          • 2011-07-26
          • 1970-01-01
          • 2011-04-19
          相关资源
          最近更新 更多