Replace repeated empty spaces with a single space use the regex (Замените повторяющиеся пустые пробелы одним пробелом, используя регулярное выражения в свифт) - Swift 5
let myString = "ALEX, Tim, Alice, TOM!!!!"
let stringWithoutWhitespace = myString.replacingOccurrences(of: "^\\s+|\\s+|\\s+$", with: " ", options: .regularExpression)