[Solved] String prefix and Suffix separated by “: ” in Swift [closed]


You can use regular expression "(?<!:) " to replace the white spaces " " occurrences where it is not preceded by colon punctuation ":":

let string = "Apple: Fruit Tomato: Vegetable Iron: Material"
let pattern = "(?<!:) "
let result = string.replacingOccurrences(of: pattern, with: "\n", options: .regularExpression)
print(result)    // "Apple: Fruit\nTomato: Vegetable\nIron: Material\n"

solved String prefix and Suffix separated by “: ” in Swift [closed]