C# Divide String array

Hi there,

I have a String[ ] array that contains full path to image files from the gallery. How can I divide the array to the parts grouping by folders. For example:

I have:
/storage/sdcard/DCIM/FirstFolder/IMG123.jpg
/storage/sdcard/DCIM/SecondFolder/IMG123.jpg
/storage/sdcard/DCIM/FirstFolder/IMG123.jpg
/storage/sdcard/DCIM/SecondFolder/IMG123.jpg
/storage/sdcard/DCIM/SecondFolder/IMG123.jpg

I need:
array1
/storage/sdcard/DCIM/FirstFolder/IMG123.jpg
/storage/sdcard/DCIM/FirstFolder/IMG123.jpg
array2
/storage/sdcard/DCIM/SecondFolder/IMG123.jpg
/storage/sdcard/DCIM/SecondFolder/IMG123.jpg
/storage/sdcard/DCIM/SecondFolder/IMG123.jpg

Have you done anything to solve this yourself or are you unsure of where to begin?

The string class should have a library of functions for working with them.

But I’ll give you a general method for solving this:

  1. Iterate through your string array, searching for the folder name within each string.
  2. If the folder is “FirstFolder” put it in array1
  3. if the folder is “Second folder” put it in array2

You can use String.contains(“FolderName”) to find the folder you’re looking for and place it in array1 or array2 since you know it exists within the path.

More info: String Class (System) | Microsoft Learn