简体   繁体   English

替换字符串数组中的特定字符

[英]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 "*".我想知道如何用“*”替换数组中的所有“e”字符。 It´s important for me to always use my array and not create a new one.对我来说始终使用我的数组而不是创建一个新数组很重要。

Any help would be appriciated.任何帮助将被appriciated。

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 :或者更简单的map解决方案:

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

Both will give you:两者都会给你:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM