1

I was wondering if there is a way in an ArrayList that I can search to see if the record contains a certain characters, If so then grab the whole entire sentence and put in into a string. For Example:

list[0] = "C:\Test3\One_Title_Here.pdf";
list[1] = "D:\Two_Here.pdf";
list[2] = "C:\Test\Hmmm_Joke.pdf";
list[3] = "C:\Test2\Testing.pdf";

Looking for: "Hmmm_Joke.pdf"
Want to get: "C:\Test\Hmmm_Joke.pdf" and put it in the Remove()

    protected void RemoveOther(ArrayList list, string Field)
    {
        string removeStr;

        -- Put code in here to search for part of a string which is Field --
        -- Grab that string here and put it into a new variable --
        list.Contains();
        list.Remove(removeStr);

    }

Hope this makes sense. Thanks.

6
  • 3
    Why are you using ArrayList over List<T> ? Commented Sep 25, 2013 at 20:16
  • cause that is what they are using. Commented Sep 25, 2013 at 20:17
  • 1
    You are blanking out the Field variable. This will clear any value passed into the function. Just to be clear, in your example you want to remove the element that contains "Hmmm_Joke.pdf" from the arrayList? Commented Sep 25, 2013 at 20:18
  • Yeah I understand that. But what I am looking for is in my list how can I search for that "Hmmm_Joke.pdf" and then grab that whole sentence "C:\Test\Hmm_Joke.pdf" and then put it in the Field string then Remove it. from the list. Commented Sep 25, 2013 at 20:20
  • Does it have to be case sensitive? Does it have to delete all files with the same name in different folders? Commented Sep 25, 2013 at 20:25

4 Answers 4

4

Loop through each string in the array list and if the string does not contain the search term then add it to new list, like this:

string searchString = "Hmmm_Joke.pdf";
ArrayList newList = new ArrayList();

foreach(string item in list)
{
    if(!item.ToLower().Contains(searchString.ToLower()))
    {
        newList.Add(item);
    }
}

Now you can work with the new list that has excluded any matches of the search string value.

Note: Made string be lowercase for comparison to avoid casing issues.

Sign up to request clarification or add additional context in comments.

6 Comments

That is what I was going to propose. If you want to do something else with it return a to the string with return item;
Perhaps it's good to make sure if it's case sensitive. I would also use LINQ to traverse the list. Since it's unlikely to know if we will have different Hmm_Joke.pdf in different folders. Ref: codeproject.com/Questions/411787/…
This code will not work, it will throw an InvalidOperationException, the ArrayList cannot be modified while it is being iterated upon.
On the foreach().... Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
Updated answer to account for InvalidOperationException.
|
1

In order to remove a value from your ArrayList you'll need to loop through the values and check each one to see if it contains the desired value. Keep track of that index, or indexes if there are many.

Then after you have found all of the values you wish to remove, you can call ArrayList.RemoveAt to remove the values you want. If you are removing multiple values, start with the largest index and then process the smaller indexes, otherwise, the indexes will be off if you remove the smallest first.

Comments

0

This will do the job without raising an InvalidOperationException:

string searchString = "Hmmm_Joke.pdf";
foreach (string item in list.ToArray())
{
    if (item.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0)
    {
        list.Remove(item);
    }
}

I also made it case insensitive.

Good luck with your task.

Comments

0

I would rather use LINQ to solve this. Since IEnumerables are immutable, we should first get what we want removed and then, remove it.

        var toDelete = Array.FindAll(list.ToArray(), s =>
            s.ToString().IndexOf("Hmmm_Joke.pdf", StringComparison.OrdinalIgnoreCase) >= 0
            ).ToList();

        toDelete.ForEach(item => list.Remove(item));

Of course, use a variable where is hardcoded.

I would also recommend read this question: Case insensitive 'Contains(string)' It discuss the proper way to work with characters, since convert to Upper case/Lower case since it costs a lot of performance and may result in unexpected behaviours when dealing with file names like: 文書.pdf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.