user4423327
user4423327

Reputation:

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

Upvotes: 1

Views: 648

Answers (2)

Abubakar Moallim
Abubakar Moallim

Reputation: 4923

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

Upvotes: 0

Kirsteins
Kirsteins

Reputation: 27345

You can use filter method:

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

Upvotes: 4

Related Questions