替换特定的字符

问题描述:

我的字符串数组包含字符串的倍数:替换特定的字符

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

我不知道如何可以代替阵列中的所有“E”字符“*”。对我来说,始终使用我的阵列而不是创建一个新阵列对我来说很重要。

任何帮助将appriciated。

+2

当然,你试过*的东西, *不是吗?不要犹豫,展示你的尝试! –

你可以做这样的事情:

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

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

或者一个简单的解决方案与map

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

都将给您:

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