0

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.

emerson.marini
  • 9,331
  • 2
  • 29
  • 46
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • See here for a good way of taking the last `n` elements from an enumerable: http://stackoverflow.com/questions/3453274/using-linq-to-get-the-last-n-elements-of-a-collection – Ric Feb 11 '16 at 09:06
  • Possible duplicate of [What's the best way to do a backwards loop in C/C#/C++?](http://stackoverflow.com/questions/275994/whats-the-best-way-to-do-a-backwards-loop-in-c-c-c) – emerson.marini Feb 11 '16 at 09:08
  • thanks, so my code would look like this: `tokens.Skip(Math.Max(0, tokens.Count() - 5));` inside the `try` but where would I `Append` the String held inside the Cookie? @Ric – Jaquarh Feb 11 '16 at 09:10

2 Answers2

1

To take the last 5 elements out of the array, you can use linq and skip the first length-5 elements like this:

foreach (String searchHist in tokens.Skip(tokens.Length - 5))
{
    if(userSettings.Length > refer)
    {
        output.Append("<li>" + searchHist + "</li>");
    }
}

You may need to check if the length is at least 5 before that:

int toSkip = tokens.Length - 5;
if (toSkip < 0) toSkip = 0;
foreach (String searchHist in tokens.Skip(toSkip))
   ...
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Brilliant, this worked! Thank-you! I'll look into the `Skip` idea for an understanding :) – Jaquarh Feb 11 '16 at 09:18
  • 1
    @KyleE4K there is no big idea behind. An array is an `IEnumerable` and `Skip` is an extension method that returns a new `IEnumerable` that skips the first _n_ elements of the source `IEnumerable`. [Here is the MSDN documentation](https://msdn.microsoft.com/en-us/library/bb358985%28v=vs.110%29.aspx) – René Vogt Feb 11 '16 at 09:20
0

Try this insted of the foreach loop if the array always look like this: first:second:third:forth:fifth:sixth and you want second, third, forth and fifth :

for (int i = tokens.Length - 6, i < tokens.Length; i++)
{
    string searchHist = tokens[i];

    if(userSettings.Length > refer)
    {
        output.Append("<li>" + searchHist + "</li>");
    }
}
M. Schena
  • 2,039
  • 1
  • 21
  • 29
  • That's just an example of what he wants to achieve. The array does not only contain 5-6 items – Jacky Feb 11 '16 at 09:15
  • ya was going to say that, it actually has a undefined X amount of things in because the person could of searched 55 things for all I know. But I appreciate the help – Jaquarh Feb 11 '16 at 09:16
  • he said: «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.» - so i did – M. Schena Feb 11 '16 at 09:16
  • @KyleE4K : you said, that in that case you need from second to fifth, what about the sixth?, this aren't the last five - or do you really need last five? – M. Schena Feb 11 '16 at 09:18