Remove elements from an array that contains particular string in swift

655 Views Asked by At

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??

2

There are 2 best solutions below

0
On BEST ANSWER

You can use filter method:

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

A simple way to do it

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"
]

var newArray: [String] = []


for i in 0...array_var.count - 1 {
if array_var[i].rangeOfString("convert?fit=crop") != nil {

    newArray.append(array_var[i])


}
}
array_var = newArray