user8162327
user8162327

Reputation: 21

Replace specific characters in String Array

My String Array contains multiples strings:

var array = ["Test", "Another Test", "Third test"]

I wonder how I can replace all the "e" characters in the array with "*". It´s important for me to always use my array and not create a new one.

Any help would be appriciated.

Upvotes: 0

Views: 3216

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

You can either do something like this:

var array = ["Test", "Another Test", "Third test"]

for (index, str) in array.enumerated() {
    array[index] = str.replacingOccurrences(of: "e", with: "*")
}

Or a simpler solution with map:

array = array.map({ $0.replacingOccurrences(of: "e", with: "*") })

Both will give you:

["T*st", "Anoth*r T*st", "Third t*st"]

Upvotes: 3

Related Questions