C# String Array Contains
Asked Answered
A

4

12

Can someone explain to me why the top part of the code works but when test is an array it doesn't?

string test = "Customer - ";
if (test.Contains("Customer"))
{
    test = "a";
}

The code below doesn't work

string[] test = { "Customer - " };
if (test.Contains("Customer"))
{
    test[0] = "a";
}
Assailant answered 5/7, 2017 at 4:20 Comment(1)
The two types - string & string[] - are different, so the .Contains method for each is a different method, even though it has the same name, and each does a different thing.Married
L
15

In first case, you call String.Contains which checks if string contains substring.
So, this condition returns true.

In the second case, you call Enumerable.Contains on string[] which checks if string array contains a specific value.
Since there is no "Customer" string in your collection, it returns false.

These are two similarly called but actually different methods of different types.

If you want to check if any string in collection contains "Customer" as a substring, then you can use LINQ .Any():

if (test.Any(s => s.Contains("Customer"))
{
    test[1] = "a";
}
Lycaon answered 5/7, 2017 at 4:23 Comment(0)
G
3

First snippet searches for string Customer inside another string Customer - which works but in second snippet string Customer is searched inside array of strings one of which is Customer -. Hence the second returns false.

This is evident from how you declare test

string test = "Customer - ";

OR

string[] test = { "Customer - " };

Notice the curly brackets. That's how you define a collection (array here)

To make second snippet work you should do

string[] test = { "Customer - " };
if (test.Any(x => x.Contains("Customer")))
{
    test[1] = "a";
}
Germany answered 5/7, 2017 at 4:30 Comment(2)
Thanks I think the second snippet is a little misleading because it's checking for an exact match and not a real contains. My test array has more values than what I stated here. How do I check the index for that if?Assailant
You need to run a plain old for loop from 0 to array.length-1. when the value is found, the loop value is the index. Other than that you can do it with linq. https://mcmap.net/q/405760/-c-array-findallindexof-which-findall-indexofGermany
N
1

Because in second case your if condition is false as you are searching for first string in array of strings. Either you can use any keyword to search for any string in array that match customer or you can search test[0] to search first string in array if it matches to customer like:

if (test[0].Contains("Customer"))
    {
        //your rest of the code here
    }


 if (test.Any(x => x.Contains("Customer")))
    {
        //your rest of the code here
    }
Nonstandard answered 5/7, 2017 at 4:32 Comment(1)
You don't understand the context of the question, so, ultimately your given answer is incorrect. Either remove or edit your answer.Doormat
A
1
string test = "Customer - ";
if (test.Contains("Customer"))
{
  test = "a";
}

In this Contains is a String Class Method it's base of IEnumerable checks

"Customer -" having Customer.

But

string[]test = { "Customer - " };
if (test.Contains("Customer"))
{
   test[1]= "a";
 }

In this Place Contains is a IEnumerable Method it's base of IEnumerable checks.So, It's Not Working You Change the Code

string[] test = { "Customer - " };
if (test.Any(x => x.Contains("Customer")))
{

}

test[1]="a" It's throw Exception because, the array position start with 0 not 1

Agan answered 5/7, 2017 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.