How to remove multiple spaces in Strings with Swift 2

后端 未结 6 1019
予麋鹿
予麋鹿 2021-02-07 01:23

Until Swift 2 I used this extension to remove multiple whitespaces:

func condenseWhitespace() -> String {
        let components = self.componentsSeparatedByC         


        
6条回答
  •  花落未央
    2021-02-07 02:14

    SWIFT 3: Cleaner version

    extension String {
        var condensedWhitespace: String {
            let components = self.components(separatedBy: .whitespacesAndNewlines)
            return components.filter { !$0.isEmpty }.joined(separator: " ")
        }
    }
    

提交回复
热议问题