我一直在更新我的一些旧代码和答案与Swift 3,但当我得到Swift字符串和索引子字符串的事情变得令人困惑。

具体来说,我尝试了以下几点:

let str = "Hello, playground"
let prefixRange = str.startIndex..<str.startIndex.advancedBy(5)
let prefix = str.substringWithRange(prefixRange)

第二行给出了如下错误

String类型的值没有成员substringWithRange

我看到String现在确实有以下方法:

str.substring(to: String.Index)
str.substring(from: String.Index)
str.substring(with: Range<String.Index>)

这些一开始让我很困惑,所以我开始摆弄索引和范围。这是子字符串的后续问题和答案。我在下面添加了一个答案来说明它们是如何使用的。


当前回答

String的特殊性在其他答案中已经讨论过了。解释一下:字符串有一个特定的索引,它不是Int类型,因为字符串元素在一般情况下没有相同的大小。因此,String不符合RandomAccessCollection,访问特定索引意味着遍历集合,这不是O(1)操作。

许多答案都提出了使用范围的变通方法,但它们会导致代码效率低下,因为它们使用的String方法(index(from:), index(:offsetBy:),…)不是O(1)。

要像在数组中一样访问字符串元素,您应该使用array:

let array = Array("Hello, world!")
let letter = array[5]

这是一个权衡,数组创建是一个O(n)操作,但数组访问是O(1)。当你想用String(数组)时,你可以转换回String。

其他回答

下面是一个函数,当提供了开始和结束索引时,返回给定子字符串的子字符串。如需完整参考资料,请浏览以下连结。

func substring(string: String, fromIndex: Int, toIndex: Int) -> String? {
    if fromIndex < toIndex && toIndex < string.count /*use string.characters.count for swift3*/{
        let startIndex = string.index(string.startIndex, offsetBy: fromIndex)
        let endIndex = string.index(string.startIndex, offsetBy: toIndex)
        return String(string[startIndex..<endIndex])
    }else{
        return nil
    }
}

这是我为处理swift中的字符串操作而创建的博客文章的链接。 swift中的字符串操作(也包括swift 4)

或者你可以在github上看到这个要点

我对Swift的字符串访问模型感到非常沮丧:所有东西都必须是索引。我想要的只是使用Int访问字符串的第I个字符,而不是笨拙的索引和推进(这恰好随着每个主要版本的发布而改变)。所以我对String做了一个扩展:

extension String {
    func index(from: Int) -> Index {
        return self.index(startIndex, offsetBy: from)
    }

    func substring(from: Int) -> String {
        let fromIndex = index(from: from)
        return String(self[fromIndex...])
    }

    func substring(to: Int) -> String {
        let toIndex = index(from: to)
        return String(self[..<toIndex])
    }

    func substring(with r: Range<Int>) -> String {
        let startIndex = index(from: r.lowerBound)
        let endIndex = index(from: r.upperBound)
        return String(self[startIndex..<endIndex])
    }
}

let str = "Hello, playground"
print(str.substring(from: 7))         // playground
print(str.substring(to: 5))           // Hello
print(str.substring(with: 7..<11))    // play

下面的所有示例都使用

var str = "Hello, playground"

斯威夫特4

在Swift 4中,字符串进行了相当大的修改。当你从String中获取子字符串时,你得到的是substring类型而不是String类型。为什么会这样?字符串是Swift中的值类型。这意味着如果你使用一个字符串来创建一个新的字符串,那么它必须被复制。这有利于稳定性(没有人会在你不知情的情况下改变它),但不利于效率。

另一方面,Substring是返回到它所来自的原始String的引用。下面是文档中的一张图片说明了这一点。

不需要复制,所以使用起来更有效率。但是,假设您从100万个字符字符串中获得了10个字符的子字符串。因为Substring引用了String,只要Substring存在,系统就必须保留整个String。因此,当你完成对Substring的操作时,将其转换为String。

let myString = String(mySubstring)

这将只复制子字符串,保留旧字符串的内存可以被回收。子字符串(作为一种类型)意味着生命周期很短。

Swift 4的另一个重大改进是字符串是集合(再次)。这意味着你可以对Collection做什么,也可以对String做什么(使用下标、遍历字符、过滤器等)。

下面的例子展示了如何在Swift中获取子字符串。

获得子字符串

您可以通过使用下标或许多其他方法(例如,前缀、后缀、split)从字符串中获得子字符串。您仍然需要使用String。索引,而不是范围的Int索引。(如果你需要帮助,请参阅我的另一个答案。)

字符串的开头

你可以使用下标(注意Swift 4的单边范围):

let index = str.index(str.startIndex, offsetBy: 5)
let mySubstring = str[..<index] // Hello

或前缀:

let index = str.index(str.startIndex, offsetBy: 5)
let mySubstring = str.prefix(upTo: index) // Hello

或者更简单:

let mySubstring = str.prefix(5) // Hello

字符串的结尾

使用下标:

let index = str.index(str.endIndex, offsetBy: -10)
let mySubstring = str[index...] // playground

或后缀:

let index = str.index(str.endIndex, offsetBy: -10)
let mySubstring = str.suffix(from: index) // playground

或者更简单:

let mySubstring = str.suffix(10) // playground

注意,当使用后缀(from: index)时,我必须使用-10从末尾开始计数。当只使用后缀(x)时,这是不必要的,它只接受字符串的最后x个字符。

字符串中的范围

同样,我们在这里使用了下标。

let start = str.index(str.startIndex, offsetBy: 7)
let end = str.index(str.endIndex, offsetBy: -6)
let range = start..<end

let mySubstring = str[range]  // play

将子字符串转换为字符串

不要忘记,当你准备保存你的子字符串时,你应该把它转换成一个字符串,这样旧的字符串的内存就可以被清理。

let myString = String(mySubstring)

使用Int索引扩展?

在阅读Airspeed Velocity和Ole Begemann的文章《Strings in Swift 3》后,我犹豫是否要使用基于Int的索引扩展。虽然在Swift 4中,字符串是集合,但Swift团队故意没有使用Int索引。它仍然是String.Index。这与Swift字符由不同数量的Unicode码点组成有关。实际索引必须为每个字符串唯一地计算。

我不得不说,我希望Swift团队能找到一种方法来抽象String。未来的指数。但在那之前,我选择使用他们的API。它帮助我记住String操作不仅仅是简单的Int索引查找。

我为此创建了一个简单的扩展(Swift 3)

extension String {
    func substring(location: Int, length: Int) -> String? {
        guard characters.count >= location + length else { return nil }
        let start = index(startIndex, offsetBy: location)
        let end = index(startIndex, offsetBy: location + length)
        return substring(with: start..<end)
    }
}

斯威夫特5 let desiredIndex: Int = 7 let substring = str[字符串]。指数(encodedOffset: desiredIndex)…] 这个子字符串变量会给你结果。 这里Int被转换为Index,然后你可以拆分字符串。除非你会得到错误。