iOS iOS - 5 months ago 13
Swift Question

Replace all characters in a String in Swift

Just trying to do a play around with Swift String. I want to replace all the characters in a String to a blank space.

inputString = "This is a String"

outputString = " "


How could I achieve this in swift?

Answer

Another way:

let outputString = inputString.stringByReplacingOccurrencesOfString("[^\\s]",
                    withString: " ", 
                       options: .RegularExpressionSearch, 
                         range: Range(inputString.startIndex..<inputString.endIndex))