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.

user2800287
  • 55
  • 3
  • 10
  • 3
    Why are you using `ArrayList` over `List` ? – Sriram Sakthivel Sep 25 '13 at 20:16
  • cause that is what they are using. – user2800287 Sep 25 '13 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? – Joseph Carrigan Sep 25 '13 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. – user2800287 Sep 25 '13 at 20:20
  • Does it have to be case sensitive? Does it have to delete all files with the same name in different folders? – Dorival Sep 25 '13 at 20:25
  • Not case sensitive. All with the same name. But it is very rare that they are the same. – user2800287 Sep 25 '13 at 20:26

4 Answers4

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.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • 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; – Joseph Carrigan Sep 25 '13 at 20:21
  • 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: http://www.codeproject.com/Questions/411787/Search-for-a-string-within-the-string-of-IEnumerab – Dorival Sep 25 '13 at 20:22
  • 1
    This code will not work, it will throw an `InvalidOperationException`, the `ArrayList` cannot be modified while it is being iterated upon. – JG in SD Sep 25 '13 at 20:24
  • On the foreach().... Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. – user2800287 Sep 25 '13 at 20:24
  • Updated answer to account for `InvalidOperationException`. – Karl Anderson Sep 25 '13 at 20:27
  • While this may work, it it worth noting, that if you cannot change the reference of the object, a different approach will be needed. – JG in SD Sep 25 '13 at 20:30
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.

JG in SD
  • 5,427
  • 3
  • 34
  • 46
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.

Casperah
  • 4,504
  • 1
  • 19
  • 13
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

Community
  • 1
  • 1
Dorival
  • 689
  • 4
  • 18