locked
Generic List with String Contains RRS feed

  • Question

  • User1858009984 posted

    I have a generic string list with below data and i have another string say "A,B,C" and i need to compare whether the String "A,B,C" contains any value in generic list, i tried below code but its working only for exact string and not for this.

    List<string> lstSpecialChar = new List<string>();
    lstSpecialChar.Add(",");
    lstSpecialChar.Add("-");
    lstSpecialChar.Add("$");

    if (lstSpecialChar.Contains("A,B,C"))
    Console.WriteLine("Yes");
    else
    Console.WriteLine("No");

    Wednesday, May 23, 2018 7:09 AM

Answers

  • User283571144 posted

    Hi Ravindranath M,

    According to your description, I suggest you could use LINQ to achieve your requirements.

    You could use any to loop the string list.

    Then if the 'A,B,C' contains the value in the list, it will return true.

    More details, you could refer to below codes:

            static void Main(string[] args)
            {
                string mystring = "A,B,C";
                List<string> lstSpecialChar = new List<string>();
                lstSpecialChar.Add(",");
                lstSpecialChar.Add("-");
                lstSpecialChar.Add("$");
                bool b = lstSpecialChar.Any(s => mystring.Contains(s));
                Console.WriteLine(b);
                Console.Read();
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 24, 2018 7:48 AM

All replies

  • User753101303 posted

    Hi,

    Likely a confusion between String.Contains(string) and List.Contains(element) which is also often seen when using Linq.

    If this is a list of characters (rather than strings) you could use  https://msdn.microsoft.com/en-us/library/System.String.IndexOfAny%28v=vs.110%29.aspx :

    var lstSpecialChar = new char[] { ',', '-', '$' };
    Console.WriteLine("A,B,C".IndexOfAny(lstSpecialChar));
    

    Or "A,B,C" should be a list ? You could then use https://msdn.microsoft.com/en-us/library/bfed8bca(v=vs.110).aspx or LINQ to objects.

    Wednesday, May 23, 2018 7:32 AM
  • User283571144 posted

    Hi Ravindranath M,

    According to your description, I suggest you could use LINQ to achieve your requirements.

    You could use any to loop the string list.

    Then if the 'A,B,C' contains the value in the list, it will return true.

    More details, you could refer to below codes:

            static void Main(string[] args)
            {
                string mystring = "A,B,C";
                List<string> lstSpecialChar = new List<string>();
                lstSpecialChar.Add(",");
                lstSpecialChar.Add("-");
                lstSpecialChar.Add("$");
                bool b = lstSpecialChar.Any(s => mystring.Contains(s));
                Console.WriteLine(b);
                Console.Read();
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 24, 2018 7:48 AM