如何在Swift中拆分字符串

时间:2014-09-12 23:14:22

标签: string swift split

let string = "hello hi"
var hello = ""
var hi = ""

我不想拆分我的字符串,以便hello的值得到"你好"和hi的值得到" hi"

2 个答案:

答案 0 :(得分:111)

试试这个:

var myString: String = "hello hi";
var myStringArr = myString.componentsSeparatedByString(" ")

其中myString是字符串的名称,myStringArr包含由空格分隔的组件。

然后你可以得到组件:

var hello: String = myStringArr [0]
var hi: String = myStringArr [1]

Doc:componentsSeparatedByString

编辑:对于Swift 3,以上将是:

var myStringArr = myString.components(separatedBy: " ")

Doc:components(separatedBy:)

答案 1 :(得分:25)

以下split也接收正则表达式。您可以定义扩展以供将来使用:

Swift 4

extension String {

    func split(regex pattern: String) -> [String] {

        guard let re = try? NSRegularExpression(pattern: pattern, options: [])
            else { return [] }

        let nsString = self as NSString // needed for range compatibility
        let stop = "<SomeStringThatYouDoNotExpectToOccurInSelf>"
        let modifiedString = re.stringByReplacingMatches(
            in: self,
            options: [],
            range: NSRange(location: 0, length: nsString.length),
            withTemplate: stop)
        return modifiedString.components(separatedBy: stop)
    }
}

示例:

let string1 = "hello world"
string1.split(regex: " ")    // ["hello", "world"]

let string2 = "hello    world"
string2.split(regex: "[ ]+")  // ["hello", "world"]

Swift 2.2

extension String {

    func split(regex pattern: String) -> [String] {

        guard let re = try? NSRegularExpression(pattern: pattern, options: []) 
            else { return [] }

        let nsString = self as NSString // needed for range compatibility
        let stop = "<SomeStringThatYouDoNotExpectToOccurInSelf>"
        let modifiedString = re.stringByReplacingMatchesInString(
            self,
            options: [],
            range: NSRange(location: 0, length: nsString.length),
            withTemplate: stop)
        return modifiedString.componentsSeparatedByString(stop)
    }
}

Swift 2.0

extension String {

    // java, javascript, PHP use 'split' name, why not in Swift? :)
    func split(regex: String) -> Array<String> {
        do{
            let regEx = try NSRegularExpression(pattern: regex, options: NSRegularExpressionOptions())
            let stop = "<SomeStringThatYouDoNotExpectToOccurInSelf>"
            let nsString = self as NSString // needed for range compatibility
            let modifiedString = regEx.stringByReplacingMatchesInString (self, options: NSMatchingOptions(), range: NSRange(location: 0, length: nsString.length), withTemplate:stop)
            return modifiedString.componentsSeparatedByString(stop)
        } catch {
            return []
        }
    }
}

Swift 1.1

extension String {

    // java, javascript, PHP use 'split' name, why not in Swift? :)
    func split(splitter: String) -> Array<String> {
        let regEx = NSRegularExpression(pattern: splitter, options: NSRegularExpressionOptions(), error: nil)
        let stop = "<SomeStringThatYouDoNotExpectToOccurInSelf>"
        let modifiedString = regEx.stringByReplacingMatchesInString (self, options: NSMatchingOptions(),
            range: NSMakeRange(0, countElements(self)),
            withTemplate:stop)
        return modifiedString.componentsSeparatedByString(stop)
    }
}

示例:

let string1 = "hello world"

string1.split(" ")    // ["hello", "world"]

let string2 = "hello    world"

string2.split("[ ]+")  // ["hello", "world"]