4

i want to replace the last occurence of <li> to add class inside it, it should look like below. <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>

i have stored the above html in a string variable. what could i do to achieve this.

Abbas
  • 4,948
  • 31
  • 95
  • 161

4 Answers4

12

If you are doing this in code behind, which is what I perceive to be the case, try someting like:

    var last = htmlString.LastIndexOf("<li>");
    htmlString = htmlString.Remove(last, 4).Insert(last, "<li class=\"last\"");
Adam Barney
  • 2,377
  • 3
  • 18
  • 24
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);
Nikola Radosavljević
  • 6,871
  • 32
  • 44
1

Maybe you can use jquery to achive this:

$("li:last").addClass("last")
jrummell
  • 42,637
  • 17
  • 112
  • 171
GRGodoi
  • 1,946
  • 2
  • 24
  • 38
  • actually i am fetching the records in html format from database, so while displaying on the page, i need to add that class, so i need to use .net – Abbas Nov 03 '11 at 18:50
0

You can do this in jQuery like this:

$("#listid li:last").addClass("last");
James Johnson
  • 45,496
  • 8
  • 73
  • 110