Skip to main content
deleted 1 character in body
Source Link
marc_s
  • 759.9k
  • 186
  • 1.4k
  • 1.5k

Swift 5+ Remove

Remove All whitespacewhitespaces from prefix(start) of the string, you can use similar for sufixsuffix/end of the string

 extension String { func deletingPrefix(_ prefix: String) -> String { guard self.hasPrefix(prefix) else { return self } return String(self.dropFirst(prefix.count)) } func removeWhitespacePrefix() -> String { let prefixString = self.prefix(while: { char in return char == " " }) return self.deletingPrefix(String(prefixString)) } } 

Swift 5+ Remove All whitespace from prefix(start) of the string, you can use similar for sufix/end of the string

 extension String { func deletingPrefix(_ prefix: String) -> String { guard self.hasPrefix(prefix) else { return self } return String(self.dropFirst(prefix.count)) } func removeWhitespacePrefix() -> String { let prefixString = self.prefix(while: { char in return char == " " }) return self.deletingPrefix(String(prefixString)) } } 

Swift 5+

Remove All whitespaces from prefix(start) of the string, you can use similar for suffix/end of the string

 extension String { func deletingPrefix(_ prefix: String) -> String { guard self.hasPrefix(prefix) else { return self } return String(self.dropFirst(prefix.count)) } func removeWhitespacePrefix() -> String { let prefixString = self.prefix(while: { char in return char == " " }) return self.deletingPrefix(String(prefixString)) } } 
Source Link
UnRewa
  • 2.5k
  • 3
  • 29
  • 33

Swift 5+ Remove All whitespace from prefix(start) of the string, you can use similar for sufix/end of the string

 extension String { func deletingPrefix(_ prefix: String) -> String { guard self.hasPrefix(prefix) else { return self } return String(self.dropFirst(prefix.count)) } func removeWhitespacePrefix() -> String { let prefixString = self.prefix(while: { char in return char == " " }) return self.deletingPrefix(String(prefixString)) } }