如何使用Swift 2删除字符串中的多个空格 [英] How to remove multiple spaces in Strings with Swift 2

查看:325
本文介绍了如何使用Swift 2删除字符串中的多个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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)
}

但是现在有了Swift 2,我得到了错误

but with Swift 2 now I get the error

无法使用类型为((String)''的参数列表调用'isEmpty'

Cannot invoke 'isEmpty' with an argument list of type '(String)'

现在如何使用Swift 2删除多个空格? 谢谢!

How could I now remove multiple spaces with Swift 2? Thnx!

推荐答案

Swift 2 中,join变为joinWithSeparator,您可以在数组上调用它.

In Swift 2, join has become joinWithSeparator and you call it on the array.

filter中,应在当前迭代项$0上调用isEmpty.

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!"


Swift 3 中,还有更多更改.


In Swift 3 there's even more changes.

功能:

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()

属性:

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

Swift 4.2 中,NSCharacterSet现在是CharacterSet,您可以省略并使用点语法:

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!"

这篇关于如何使用Swift 2删除字符串中的多个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆