0

Say I have an array of strings

string[] my_array = { "XY:1234567;ZW:124", "XY:124252"};

And a user inputs string user_input = "1234567";

how can I remove the entire string from my_array

so after remove XY:1234567;ZQ:124 is removed based on the partial input

my_array = { "XY:124252" };

My first attempt:

string[] my_array = { "XY:12345678;ZW:124", "XY:124252" };
string user_input = "1234567";
if(my_array.Contains(user_input))
    Console.WriteLine("Inside");
else
    Console.WriteLine("Not Inside");

Outputted Not Inside

My second attempt:

string[] my_array = { "XY:12345678;ZW:124", "XY:124252" };
string user_input = "%1234567%"; // Tried % to do a partial string I think?
if(my_array.Contains(user_input))
    Console.WriteLine("Inside");
else
    Console.WriteLine("Not Inside");

Outputted Not Inside

Im out of ideas

3
  • 3
    array.Contains checks if the array contains the entire string. You want array.Any(s => s.Contains(...)). Commented Feb 19, 2020 at 14:10
  • How does 124 partially matches 1234567? Commented Feb 19, 2020 at 14:10
  • You shoul'd use my_array.Any(s => s.Contains(user_input)) Commented Feb 19, 2020 at 14:16

1 Answer 1

1

you need String.Contains(). Try like:

string[] my_array = { "XY:12345678;ZW:124", "XY:124252" };
string user_input = "1234567";
foreach( var item in my_array)
{
   if(item.Contains(user_input))
      Console.WriteLine("Inside");
   else
      Console.WriteLine("Not Inside");
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.