假设我这里有一个字符串:
var fullName: String = "First Last"
我想在空白处拆分字符串,并将值分配给它们各自的变量
var fullNameArr = // something like: fullName.explode(" ")
var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
此外,有时用户可能没有姓氏。
假设我这里有一个字符串:
var fullName: String = "First Last"
我想在空白处拆分字符串,并将值分配给它们各自的变量
var fullNameArr = // something like: fullName.explode(" ")
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))
其他回答
或者没有闭包,你可以在Swift 2中这样做:
let fullName = "First Last"
let fullNameArr = fullName.characters.split(" ")
let firstName = String(fullNameArr[0])
我在寻找loosy拆分,例如PHP的分解,其中空序列包含在结果数组中,这对我很有用:
"First ".split(separator: " ", maxSplits: 1, omittingEmptySubsequences: false)
输出:
["First", ""]
最简单的解决方案是
let fullName = "First Last"
let components = fullName.components(separatedBy: .whitespacesAndNewlines).compactMap { $0.isEmpty ? nil : $0 }
这将处理不同类型的一行中的多个空格(空格、制表符、换行符等),并且只返回一个两元素数组,您可以更改CharacterSet以包含更多您喜欢的字符,如果您想获得切割符,可以使用正则表达式解码器,这允许您编写正则表达式,该表达式可用于将字符串直接解码到实现解码协议的自己的类/结构中。对于像这样的东西来说,这是过度的,但如果您将其用作更复杂字符串的示例,则可能更有意义。
根据Swift 2.2
您只需编写2行代码,就会得到拆分字符串。
let fullName = "FirstName LastName"
var splitedFullName = fullName.componentsSeparatedByString(" ")
print(splitedFullName[0])
print(splitedFullName[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("")