none
How to promptly compare if string exists in List<string> RRS feed

  • Question

  • I have to compare a string if it exists in List<string> fastly?

    That string is message ID of which length is 38.

    Is there any other way to replace with List<string> to compare fastly?

    Tuesday, June 4, 2019 3:47 AM

Answers

  • Since the values are unique, consider these classes:

     

    • HashSet<string>
    • SortedSet<string>

     

    Use Contains function.

     

    You can give the results of your investigations for all of three collections and large amount of data.

    • Marked as answer by Jeff0803 Tuesday, June 4, 2019 12:14 PM
    Tuesday, June 4, 2019 4:46 AM
  • Hi Jeff0803,

    Thank you for posting here.

    Based on your description, you want to determine if the string is in the list. You could refer to Viorel' advice, it is best for you to use List<T>.Contains(T) method.

    I make a simple code for this, you could have a look.

    static void Main(string[] args)
            {
                string ID = "QWERASDFZXCV1234567890QWERASDFZXCV1234567890";
                List<string> list = new List<string>();
                list.Add(ID);
                bool t = Ifstringexists(ID, list);
                list.Clear();
                list.Add("tet");
                bool m = Ifstringexists(ID, list);
    
    
            }
            static bool Ifstringexists(string test,List<string> list)
            {
                if(list.Contains(test))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Tuesday, June 4, 2019 12:15 PM
    Tuesday, June 4, 2019 5:27 AM
    Moderator

All replies

  • Since the values are unique, consider these classes:

     

    • HashSet<string>
    • SortedSet<string>

     

    Use Contains function.

     

    You can give the results of your investigations for all of three collections and large amount of data.

    • Marked as answer by Jeff0803 Tuesday, June 4, 2019 12:14 PM
    Tuesday, June 4, 2019 4:46 AM
  • Hi Jeff0803,

    Thank you for posting here.

    Based on your description, you want to determine if the string is in the list. You could refer to Viorel' advice, it is best for you to use List<T>.Contains(T) method.

    I make a simple code for this, you could have a look.

    static void Main(string[] args)
            {
                string ID = "QWERASDFZXCV1234567890QWERASDFZXCV1234567890";
                List<string> list = new List<string>();
                list.Add(ID);
                bool t = Ifstringexists(ID, list);
                list.Clear();
                list.Add("tet");
                bool m = Ifstringexists(ID, list);
    
    
            }
            static bool Ifstringexists(string test,List<string> list)
            {
                if(list.Contains(test))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Tuesday, June 4, 2019 12:15 PM
    Tuesday, June 4, 2019 5:27 AM
    Moderator
  • You can use Linq to query the List<string>.

    https://www.tutorialsteacher.com/linq/what-is-linq

    https://www.tutorialspoint.com/linq/linq_lambda_expressions.htm

    var hit = (from a in List.where(x => x.contains('test') select a).FirstOrDefault();
    
    if(hit != null)
    {
         //the string was found
    }
    else
    {
       //the string was not found
    }



    • Edited by DA924x Tuesday, June 4, 2019 8:58 AM
    Tuesday, June 4, 2019 8:57 AM