Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to check of List<> contains ONLY specific strings?

Discussion in 'Scripting' started by aracadag, Nov 10, 2015.

  1. aracadag

    aracadag

    Joined:
    Sep 15, 2015
    Posts:
    3
    Hello, I've been looking online and I cannot find an answer (I've tried phrasing it different ways, but nothing).
    I'm working with the List<> class in C#. I want to check if a List contains ONLY certain strings, so it will return true if only those strings are in the List, and false if even one string does not match.

    So far, I've been doing this with an if statement that checks booleans for each possible string. Here is my code:

    if (List.Contains ("A") &&
    List.Contains ("B") &&
    !List.Contains ("C") &&
    !List.Contains ("D"))
    {
    // do something
    }


    ...however, while this is great only as long as the list of possible strings is very short. Imagine if there are 10000 different names, for instance, that could possibly populate the List, but I just want to check if the List ONLY contains Bob and Amy... using an if statement and checking booleans, I would have to set up conditionals for the other 9998 names!

    There's gotta be a simpler way to do this. Something akin to: if(List.ONLYcontains("A" && "B")){} But so far nothing...

    Thank you!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Linq might be able to do this simply.

    Otherwise you will simply need to iterate through each item on the list and check it against your conditions.
     
    aracadag likes this.
  3. Wowo51

    Wowo51

    Joined:
    Oct 12, 2015
    Posts:
    25
    Untested draft code!!!

    Code (CSharp):
    1.         public static Boolean ContainsOnly(List<String> fullList, List<String> stringsThatMustBeContained)
    2.         {
    3.             Int32 fullListIndex;
    4.             Int32 containsIndex;
    5.             Boolean contains = false;
    6.             for (fullListIndex = 0; fullListIndex < fullList.Count; fullListIndex++)
    7.             {
    8.                 contains = false;
    9.                 for (containsIndex = 0; containsIndex < stringsThatMustBeContained.Count; containsIndex++)
    10.                 {
    11.                     if (fullList[fullListIndex] == stringsThatMustBeContained[containsIndex])
    12.                     {
    13.                         contains = true;
    14.                         break;
    15.                     }
    16.                 }
    17.                 if (contains == false)
    18.                 {
    19.                     return false;
    20.                 }
    21.             }
    22.             return true;
    23.         }
     
    aracadag and Kiwasi like this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    It's pretty simple if you phrase it as: does my list contain anything not in the "approved" (those specific strings) list?

    Then it's just a matter of checking each item in your list: if it's not in the approved list, then you know it doesn't contain only approved items.

    Code (CSharp):
    1. bool ContainsOnly() {
    2.     List<string> approved = new List<string> { "Bob", "Amy" };
    3.     foreach (item in myNames) {
    4.         if (!approved.Contains(item)) return false;
    5.     }
    6.     return true;
    7. }
    So this returns true if the list contains ONLY names in the approved list, and false it if contains anything else.
     
    sodonikagames, aracadag and Kiwasi like this.
  5. aracadag

    aracadag

    Joined:
    Sep 15, 2015
    Posts:
    3
    Thanks everyone. Seems so simple now!