Abbas
Abbas

Reputation: 5044

How to replace the last occurence of the character in .net

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.

Upvotes: 4

Views: 5165

Answers (4)

Adam Barney
Adam Barney

Reputation: 2387

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\"");

Upvotes: 12

Nikola Radosavljević
Nikola Radosavljević

Reputation: 6911

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

Upvotes: 2

GRGodoi
GRGodoi

Reputation: 1996

Maybe you can use jquery to achive this:

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

Upvotes: 1

James Johnson
James Johnson

Reputation: 46077

You can do this in jQuery like this:

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

Upvotes: 0

Related Questions