Search code examples
macosswiftswift-array

Remove elements from an array that contains particular string in swift


I have an array.

var array_var: [String] = [
"https://www.filepicker.io/api/file/XXXXXXXXXXXXX/convert?fit=crop&w=200&h=200&rotate=exif",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXX/convert?fit=crop&w=320&h=300&rotate=exif",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXXXX",
"https://www.filepicker.io/api/file/XXXXXXXXXXXXXXXX"
]

So I just want to remove(exclude) the elements that contains the string convert?fit=crop from the array.

So how can we remove them by using Swift??


Solution

  • You can use filter method:

    array_var = array_var.filter {
        $0.rangeOfString("convert?fit=crop") == nil
    }