我有一个包含单词和空格的字符串“2h 3m 1s”。我想提取2h
脱离它;所以在第一个空格之前得到所有的东西。
var date = "1,340d 1h 15m 52s" // I want to extract "1,340d".
这样做的最佳实践是什么?什么子字符串函数是最好的方法?
如果你的弦很重,componentsSeparatedByString()
往往会更快。
Swift 2:
var date = "1,340d 1h 15m 52s"
if let first = date.componentsSeparatedByString(" ").first {
// Do something with the first component.
}
斯威夫特 3/4/5:
if let first = date.components(separatedBy: " ").first {
// Do something with the first component.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)