This question already has an answer here:
这个问题已经有了答案:
- Swift: Split a String into an array 26 answers
- Swift:将字符串分割成一个数组26个答案
let string = "hello hi"
var hello = ""
var hi = ""
I wan't to split my string so that the value of hello get "hello" and the value of hi get "hi"
我不想分割字符串,这样hello的值就能得到hello, hi的值能得到hi
2 个解决方案
#1
106
Try this:
试试这个:
var myString: String = "hello hi";
var myStringArr = myString.componentsSeparatedByString(" ")
Where myString
is the name of your string, and myStringArr
contains the components separated by the space.
其中myString是字符串的名称,stringmyarr包含由空格分隔的组件。
Then you can get the components as:
然后你可以得到以下组件:
var hello: String = myStringArr [0]
var hi: String = myStringArr [1]
Doc: componentsSeparatedByString
道格:componentsSeparatedByString
EDIT: For Swift 3, the above will be:
编辑:对于Swift 3,上面将是:
var myStringArr = myString.components(separatedBy: " ")
道格:组件(separatedBy:)
#2
20
Here are split
that receives regex as well. You can define extension for future usage:
这里还有接收regex的分割。您可以为将来的使用定义扩展:
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)
}
}
Examples:
例子:
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)
}
}
Examples:
例子:
let string1 = "hello world"
string1.split(" ") // ["hello", "world"]
let string2 = "hello world"
string2.split("[ ]+") // ["hello", "world"]
#1
106
Try this:
试试这个:
var myString: String = "hello hi";
var myStringArr = myString.componentsSeparatedByString(" ")
Where myString
is the name of your string, and myStringArr
contains the components separated by the space.
其中myString是字符串的名称,stringmyarr包含由空格分隔的组件。
Then you can get the components as:
然后你可以得到以下组件:
var hello: String = myStringArr [0]
var hi: String = myStringArr [1]
Doc: componentsSeparatedByString
道格:componentsSeparatedByString
EDIT: For Swift 3, the above will be:
编辑:对于Swift 3,上面将是:
var myStringArr = myString.components(separatedBy: " ")
道格:组件(separatedBy:)
#2
20
Here are split
that receives regex as well. You can define extension for future usage:
这里还有接收regex的分割。您可以为将来的使用定义扩展:
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)
}
}
Examples:
例子:
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)
}
}
Examples:
例子:
let string1 = "hello world"
string1.split(" ") // ["hello", "world"]
let string2 = "hello world"
string2.split("[ ]+") // ["hello", "world"]