繁体   English   中英

如何替换.net中字符的最后一次出现

[英]How to replace the last occurence of the character in .net

我想替换<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存储在字符串变量中。 我能做些什么才能实现这一目标。

如果你在后面的代码中执行此操作,这就是我认为的情况,请尝试以下方法:

    var last = htmlString.LastIndexOf("<li>");
    htmlString = htmlString.Remove(last, 4).Insert(last, "<li class=\"last\"");
string data = ...;
string toReplace ="<li>";
string toPlace = "<li class=\"last\">";
int idx = data.LastIndexOf(toReplace);
data = data.Remove(idx, toReplace.Length).Insert(idx, toPlace);

也许你可以使用jquery来实现这个:

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

你可以在jQuery中这样做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM