Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取一串单词中的第一个单词&;空格-空格前的第一个字的子字符串_Ios_Swift - Fatal编程技术网

Ios 获取一串单词中的第一个单词&;空格-空格前的第一个字的子字符串

Ios 获取一串单词中的第一个单词&;空格-空格前的第一个字的子字符串,ios,swift,Ios,Swift,我有一个包含单词和空格的字符串,“2h 3m 1s”。我想从中提取2h;所以,在第一个空间之前,把一切都准备好 var date = "1,340d 1h 15m 52s" // I want to extract "1,340d". 这样做的最佳实践是什么?什么子字符串函数是最好的方法?在此上下文中,无论是.componentsSeparatedByString(“”方法还是characters.split(“”方法都不是“最好的方法”。这两种方法都将遍历完整的String对象,并给出一个

我有一个包含单词和空格的字符串,“2h 3m 1s”。我想从中提取
2h
;所以,在第一个空间之前,把一切都准备好

var date = "1,340d 1h 15m 52s"  // I want to extract "1,340d".

这样做的最佳实践是什么?什么子字符串函数是最好的方法?

在此上下文中,无论是
.componentsSeparatedByString(“”
方法还是
characters.split(“”
方法都不是“最好的方法”。这两种方法都将遍历完整的
String
对象,并给出一个
String
数组作为结果。只有在计算完数组之后,我们才能提取该数组的第一个条目。如果我们处理的是一个巨大的字符串,这在上下文中是非常不必要的,并且会导致不必要的开销

相反,对于一个巨大的字符串,最好使用以下方法:

let firstDateEntryFast = date.substringToIndex((date.rangeOfString(" ")?.first)!)
这将查找第一次出现的
”的索引,然后返回从原始字符串开始到第一次出现的子字符串。也就是说,它将永远不会调查或使用超出第一次出现点的原始字符串(在此上下文中:假定较大)

但是,您应该注意,由于强制展开(运算符(!),如果字符串不包含任何
的实例,则在运行时会崩溃。因此,为了安全起见,并遵循Swift的可选约定,请在
if let
子句中使用它:

if let myRange = date.rangeOfString(" ") {
    let firstDateEntryFast = date.substringToIndex(myRange.first!)
        // at this point, you know myRange is non-empty, and hence, .first can be
        // force-unwrapped
}
else {
    let firstDateEntryFast = date
        // no " " separation, unexpected? -> action
}

在我的第一版答案中,
split
可以用作替代方案(与
组件通过string
分离相比):

或者,跳过将它们全部存储在一个数组中,直接获取第一个条目

var date = "1,340d 1h 15m 52s"
let firstDateEntryDirectly = String(date.characters.split(" ")[0])

根据您的要求使用以下代码

var date = "1,340d 1h 15m 52s"
print((date.componentsSeparatedByString(" ") as NSArray).objectAtIndex(0));

如果字符串很重,
componentsSeparatedByString()
往往更快

Swift 2:

var date = "1,340d 1h 15m 52s"
if let first = date.componentsSeparatedByString(" ").first {
    // Do something with the first component.
}
if let first = date.components(separatedBy: " ").first {
    // Do something with the first component.
}
Swift 3/4/5:

var date = "1,340d 1h 15m 52s"
if let first = date.componentsSeparatedByString(" ").first {
    // Do something with the first component.
}
if let first = date.components(separatedBy: " ").first {
    // Do something with the first component.
}
Swift 3版本

var str = "Hello, playground one two three"
let firstWord = str.components(separatedBy: " ").first

Swift 5

var name = ""
if let optionalName = someOptionalString as? String {
    name = optionalName.contains(" ") ? (name.components(separatedBy: " ").first ?? "") : optionalName
}
首先,我们确保字符串不是nil。
然后,我们使用三元运算符根据它相应地分配它,它是否包含任何空格。

stringbytrimmingcharacters@UmaMadhavi我不只是想去掉所有的空白。我想获取第一个空格之前的字符。使用(date.componentedbystring(“”)as!NSArray)[0]请查找此内容。我想我应该指出,在这种特定情况下,接受的答案并不是最好的方法。对于大字符串,
split
componentSeparatedByString
都会在您只想将子字符串从起始位置一直保留到给定字符(“”)时产生不必要的开销。更好的方法!谢谢!回答得好!请记住,在本例中,如果日期字符串为空,则会崩溃。您可以使用可选链接来避免这种情况。如果单词之间用新行分隔,是否可以修改此选项以仅返回第一个单词?这将适用于线路返回。