Before using replacingOccurrences (of: with :) If you create a dictionary, you can "replace multiple character strings".
var str = "ABCDEF"
var newStr: String {
let dictionary = ["A": "1","B": "2","C": "3","D": "4","E": "5","F": "6"]
return dictionary.reduce(str) { $0.replacingOccurrences(of: $1.key, with: $1.value) }
}
print(newStr) // 123456
Recommended Posts