How to replace the last occurrence of a character in .net

I want to replace the last appearance of <li> , to add a class inside it, it should look like this. <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 US 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> 

I saved the above html in a string variable. what can i do for this.

+4
source share
4 answers

If you do this in the code behind, this is what I perceive as follows, try the following:

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

Perhaps you can use jquery to achieve this:

 $("li:last").addClass("last") 
+1
source

You can do this in jQuery as follows:

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

Source: https://habr.com/ru/post/1379533/


All Articles