Until Swift 2 I used this extension to remove multiple whitespaces:
func condenseWhitespace() -> String { let components = self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).filter({!Swift.isEmpty($0)}) return " ".join(components) }
but with Swift 2 now I get the error
Cannot invoke 'isEmpty' with an argument list of type '(String)'
How could I now remove multiple spaces with Swift 2? Thnx!
To remove all leading whitespaces, use the following code: var filtered = "" var isLeading = true for character in string { if character. isWhitespace && isLeading { continue } else { isLeading = false filtered.
Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.
To remove leading and trailing spaces, we use the trimmingCharacters(in:) method that removes all characters in provided character set. In our case, it removes all trailing and leading whitespaces, and new lines.
To remove the leading and trailing whitespaces from a string, we can use the trimmingCharacters(in:) method by passing a whitespaces character set offered by the swift.
In Swift 2, join
has become joinWithSeparator
and you call it on the array.
In filter
, isEmpty
should be called on the current iteration item $0
.
To replace whitespaces and newline characters with unique space characters as in your question:
extension String { func condenseWhitespace() -> String { let components = self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) return components.filter { !$0.isEmpty }.joinWithSeparator(" ") } } let result = "Hello World.\nHello!".condenseWhitespace() // "Hello World. Hello!"
Because your function does not take any parameter you could make it a property instead:
extension String { var condensedWhitespace: String { let components = self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) return components.filter { !$0.isEmpty }.joinWithSeparator(" ") } } let result = "Hello World.\nHello!".condensedWhitespace // "Hello World. Hello!"
In Swift 3 there's even more changes.
Function:
extension String { func condenseWhitespace() -> String { let components = self.components(separatedBy: NSCharacterSet.whitespacesAndNewlines) return components.filter { !$0.isEmpty }.joined(separator: " ") } } let result = "Hello World.\nHello!".condenseWhitespace()
Property:
extension String { var condensedWhitespace: String { let components = self.components(separatedBy: NSCharacterSet.whitespacesAndNewlines) return components.filter { !$0.isEmpty }.joined(separator: " ") } } let result = "Hello World.\nHello!".condensedWhitespace
In Swift 4.2 NSCharacterSet is now CharacterSet, and you can omit and use dot syntax:
extension String { func condenseWhitespace() -> String { let components = self.components(separatedBy: .whitespacesAndNewlines) return components.filter { !$0.isEmpty }.joined(separator: " ") } } let result = "Hello World.\nHello!".condenseWhitespace() // "Hello World. Hello!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With