user8162327 user8162327 -3 years ago 49
Swift Question

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.

Answer Source

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"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download