Search code examples
c#arrayscontains

Remove string Array that contain certain character


I have an array and a variable :

string[] StringArray = { "foo bar foo $ bar $ foo bar $" };
string check = "$";

And I want to remove the "$" so the output will be :

foo bar foo bar foo bar

Solution

  • If you are trying to initialize an array with multiple string, then your initialization is wrong, if that is the case, this implementation should work:

            string[] StringArray = { "foo", "bar", "foo", "$", "bar", "$", "foo", "bar", "$" };
            string check = "$";
    
            var stringList = StringArray.ToList();
    
            stringList.RemoveAll(x => x == check);
    
            StringArray = stringList.ToArray();