假设我这里有一个字符串:

var fullName: String = "First Last"

我想在空白处拆分字符串,并将值分配给它们各自的变量

var fullNameArr = // something like: fullName.explode(" ") 

var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]

此外,有时用户可能没有姓氏。


当前回答

我发现了一个有趣的案例

方法1

var data:[String] = split( featureData ) { $0 == "\u{003B}" }

当我使用此命令从从服务器加载的数据中拆分一些符号时,它可以在模拟器中测试时拆分,并与测试设备同步,但不会在发布应用程序和Ad Hoc中拆分

我花了很多时间来跟踪这个错误,它可能会被某些Swift版本或iOS版本诅咒,或者两者都没有

这也与HTML代码无关,因为我尝试stringByRemovangPercentEncoding,但它仍然不起作用

2015年10月10日新增

在Swift 2.0中,此方法已更改为

var data:[String] = featureData.split {$0 == "\u{003B}"}

方法2

var data:[String] = featureData.componentsSeparatedByString("\u{003B}")

当我使用此命令时,它可以正确分割从服务器加载的相同数据


结论,我真的建议使用方法2

string.componentsSeparatedByString("")

其他回答

或者没有闭包,你可以在Swift 2中这样做:

let fullName = "First Last"
let fullNameArr = fullName.characters.split(" ")
let firstName = String(fullNameArr[0])

我发现了一个有趣的案例

方法1

var data:[String] = split( featureData ) { $0 == "\u{003B}" }

当我使用此命令从从服务器加载的数据中拆分一些符号时,它可以在模拟器中测试时拆分,并与测试设备同步,但不会在发布应用程序和Ad Hoc中拆分

我花了很多时间来跟踪这个错误,它可能会被某些Swift版本或iOS版本诅咒,或者两者都没有

这也与HTML代码无关,因为我尝试stringByRemovangPercentEncoding,但它仍然不起作用

2015年10月10日新增

在Swift 2.0中,此方法已更改为

var data:[String] = featureData.split {$0 == "\u{003B}"}

方法2

var data:[String] = featureData.componentsSeparatedByString("\u{003B}")

当我使用此命令时,它可以正确分割从服务器加载的相同数据


结论,我真的建议使用方法2

string.componentsSeparatedByString("")

在Swift 4中将字符串拆分为数组的步骤。

赋值字符串基于@spling。

注意:variableName.components(separatedBy:“split关键字”)

let fullName: String = "First Last @ triggerd event of the session by session storage @ it can be divided by the event of the trigger."
let fullNameArr = fullName.components(separatedBy: "@")
print("split", fullNameArr)

将字符串拆分为数组的简单方法

var fullName:String=“First Last”;var fullNameArr=fullName.componentsSeparatedByString(“”)var firstName:String=fullNameArr[0]var lastName:String=fullNameArr[1]

在Swift 4.2和Xcode 10中

//This is your str
let str = "This is my String" //Here replace with your string

选项1

let items = str.components(separatedBy: " ")//Here replase space with your value and the result is Array.
//Direct single line of code
//let items = "This is my String".components(separatedBy: " ")
let str1 = items[0]
let str2 = items[1]
let str3 = items[2]
let str4 = items[3]
//OutPut
print(items.count)
print(str1)
print(str2)
print(str3)
print(str4)
print(items.first!)
print(items.last!)

选项2

let items = str.split(separator: " ")
let str1 = String(items.first!)
let str2 = String(items.last!)
//Output
print(items.count)
print(items)
print(str1)
print(str2)

选项3

let arr = str.split {$0 == " "}
print(arr)

选项4

let line = "BLANCHE:   I don't want realism. I want magic!"
print(line.split(separator: " "))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"

通过Apple文档。。。。

let line = "BLANCHE:   I don't want realism. I want magic!"
print(line.split(separator: " "))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"

print(line.split(separator: " ", maxSplits: 1))//This can split your string into 2 parts
// Prints "["BLANCHE:", "  I don\'t want realism. I want magic!"]"

print(line.split(separator: " ", maxSplits: 2))//This can split your string into 3 parts

print(line.split(separator: " ", omittingEmptySubsequences: false))//array contains empty strings where spaces were repeated.
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"

print(line.split(separator: " ", omittingEmptySubsequences: true))//array not contains empty strings where spaces were repeated.
print(line.split(separator: " ", maxSplits: 4, omittingEmptySubsequences: false))
print(line.split(separator: " ", maxSplits: 3, omittingEmptySubsequences: true))