I have managed to split a Cookie saved from recent searches into an array of all recent searches held, I now want to only bring out the last 5 searched which will be the most recent in the array
So far I have:
var output = new StringBuilder();
if (Request.Cookies["UserSettings"] != null)
{
string userSettings = Request.Cookies["UserSettings"].Value;
output.Append("<div style='float: right; width: 55%;'> Your recently searched: <ul>");
{
try
{
string[] tokens = userSettings.Split(':');
int count = 0;
int refer = userSettings.Length - 5;
foreach (String searchHist in tokens)
{
if(userSettings.Length > refer)
{
if (count > 4)
{
output.Append("<li>" + searchHist + "</li>");
}
count++;
}
}
}
catch (Exception ex)
{
output.Append("<li>" + userSettings + "</li>");
}
finally
{
output.Append("</div>");
recentSearch.Text = output.ToString();
}
}
}
But this seems to only be skipping the first 5 in the array. Is there an easier way of pulling out the last 5 strings held in an array? Many thanks.
Edit: The array looks like this - first:second:third:forth:fifth:sixth
so in this case, I need it to bring out: second third forth
and fifth
.