Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove multiple spaces in Strings with Swift 2

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!

like image 698
headkit Avatar asked Oct 10 '15 20:10

headkit


People also ask

How do I remove all spaces from a String in Swift?

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.

How do I remove extra spaces between strings?

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.

How do you remove trailing spaces in Swift?

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.

How do you trim white spaces in Swift?

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.


1 Answers

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!" 
like image 137
Eric Aya Avatar answered Sep 19 '22 09:09

Eric Aya