title: What is the shortest code to compare two comma-separated strings for a match?
body: <p>The method below called <strong>UserCanAccessThisPage</strong> is based on the following logic: each user and each page has a list of groups. If any of these match, the user has access to the page.</p>
  
  <p>The code below does what I want, but <strong>my solution is very C# 1</strong> (or C# 2, at least I didn't use ArrayList).</p>
  
  <p>Can anyone <strong>refactor this</strong> so it is more straight-forward, e.g. using lambdas to do away with the two methods? I just can't get the syntax to do it.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace TestCompare2343
  {
      class Program
      {
          static void Main(string[] args)
          {
  
              string anonymousUserAccessGroups = "loggedOutUsers";
              string normalUserAccessGroups = "loggedInUsers, members";
              string developerUserAccessGroups = "loggedInUsers, members, administrators, developers";
  
              string loginPageAccessGroups = "loggedOutUsers";
              string logoutPageAccessGroups = "loggedInUsers";
              string memberInfoPageAccessGroups = "members";
              string devPageAccessGroups = "developers";
  
              //test anonymousUser
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, loginPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, logoutPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, memberInfoPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, devPageAccessGroups));
              Console.WriteLine("---");
  
              //test anonymousUser
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, loginPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, logoutPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, memberInfoPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, devPageAccessGroups));
              Console.WriteLine("---");
  
              //test anonymousUser
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, loginPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, logoutPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, memberInfoPageAccessGroups));
              Console.WriteLine(StringHelpers.UserCanAccessThisPage(developerUserAccessGroups, devPageAccessGroups));
              Console.WriteLine("---");
  
              Console.ReadLine();
  
          }
      }
  
      public class StringHelpers
      {
          public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups)
          {
              List&lt;string&gt; userAccessGroupsList = StringHelpers.SplitAndTrimCommaDelimitedString(userAccessGroups);
              List&lt;string&gt; pageItemAccessGroupList = StringHelpers.SplitAndTrimCommaDelimitedString(pageItemAccessGroups);
  
              foreach (string userAccessGroup in userAccessGroupsList)
              {
                  foreach (string pageItemAccessGroup in pageItemAccessGroupList)
                  {
                      if (userAccessGroup == pageItemAccessGroup)
                          return true;
                  }
              }
  
              return false;
          }
  
          public static List&lt;string&gt; SplitAndTrimCommaDelimitedString(string line)
          {
              List&lt;string&gt; piecesWithSpaces = line.Split(',').ToList&lt;string&gt;();
              List&lt;string&gt; piecesWithoutSpaces = new List&lt;string&gt;();
              foreach (string pieceWithSpace in piecesWithSpaces)
              {
                  piecesWithoutSpaces.Add(pieceWithSpace.Trim());
              }
              return piecesWithoutSpaces;
          }
      }
  }
  </code></pre>
  
  <h1>Answer:</h1>
  
  <p>Fredrik had the most concise code that solved the original task above:</p>
  
  <pre><code>public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups)
  {
      return userAccessGroups
          .Split(',')
          .Select(s =&gt; s.Trim())
          .Contains(pageItemAccessGroups);
  }
  </code></pre>
  
  <h1>The code I used:</h1>
  
  <p>But Shaul was correct in assuming that the PageItems can also have multiple entries, e.g. "members,guests", and so I actually used Shaul's code:</p>
  
  <pre><code>public static bool UserCanAccessThisPage(string userAccessGroups, string pageItemAccessGroups) {
    List&lt;string&gt; userAccessGroupsList = StringHelpers.SplitAndTrimCommaDelimitedString(userAccessGroups);
    List&lt;string&gt; pageItemAccessGroupList = StringHelpers.SplitAndTrimCommaDelimitedString(pageItemAccessGroups);
    return userAccessGroupsList.Any(userAccessGroup =&gt; pageItemAccessGroupList.Any(pageItemAccessGroup =&gt; userAccessGroup == pageItemAccessGroup));
  }
  
  public static List&lt;string&gt; SplitAndTrimCommaDelimitedString(string line) {
    return line.Split(',').Select(s =&gt; s.Trim()).ToList();
  }
  </code></pre>