gpt4 book ai didi

swift - Swift 4 中更简单的字符串切片

转载 作者:搜寻专家 更新时间:2023-10-31 21:59:00 24 4
gpt4 key购买 nike

Swift 4 改变了字符串的工作方式。但是,似乎变得更加复杂且可读性降低。谁能简化这个例子(简单地将字符串的第三个字母作为字符串)? (除了拆分线。)

let myString="abc"
let thirdLetter = String(myString[myString.index(myString.startIndex, offsetBy: 2)])

最佳答案

在 Swift 4 中,您可以使用 Array(myString)String myString 转换为 [Character]。然后,您可以使用 Int 索引该数组,然后将 [Character] 转换为 String

let myString = "abc"
let thirdLetter = String(Array(myString)[2]) // "c"
let firstTwo = String(Array(myString)[0..<2]) // "ab"

如果您要对 String 进行大量操作,通常最好将其转换并保留为 [Character]


注意:我修改了这一部分,以尽量避免编译器可能进行的任何缓存优化。现在每种方法只测量一次,并为每种方法保留一个运行总数。

转换为Array 并使用Int 进行索引很容易编写和读取,但它的性能如何呢?为了回答这个问题,我在发布版本中测试了以下内容:

func time1(str: String, n: Int) -> (Double, String) {
// Method 1: Index string with String.Index, convert to String

let start = Date()
let a = String(str[str.index(str.startIndex, offsetBy: n)])
let interval = Date().timeIntervalSince(start)

return (interval, a)
}

func time2(str: String, n: Int) -> (Double, String) {
// Method 2: Convert string to array, index with Int, convert to String

let start = Date()
let a = String(Array(str)[n])
let interval = Date().timeIntervalSince(start)

return (interval, a)
}


func time3(str: String, n: Int) -> (Double, String) {
// Method 3: Use prefix() and last(), convert to String

let start = Date()
let a = String(str.prefix(n + 1).last!)
let interval = Date().timeIntervalSince(start)

return (interval, a)
}

func time4(str: String, n: Int) -> (Double, String) {
// Method 4: Use Leo Dabus' extensions
// https://stackoverflow.com/q/24092884/1630618

let start = Date()
let a = str[n]
let interval = Date().timeIntervalSince(start)

return (interval, a)
}

func time5(str: String, n: Int) -> (Double, String) {
// Method 5: Same as 2 but don't measure Array conversion time

let arr = Array(str)

let start = Date()
let a = String(arr[n])
let interval = Date().timeIntervalSince(start)

return (interval, a)
}

func test() {
for repetitions in [1, 10, 100, 1000] {
var input = ""
for _ in 0 ..< repetitions {
input.append("abcdefghijklmnopqrstuvwxyz")
}

var t = [0.0, 0.0, 0.0, 0.0, 0.0]
let funcs = [time1, time2, time3, time4, time5]

for i in 0 ..< input.count {
for f in funcs.indices {
let (interval, _) = funcs[f](input, i)
t[f] += interval
}
}

print("For string length \(input.count):")
for i in 0 ..< 5 {
print(String(format: "Method %d time: %.8f", i + 1, t[i]))
}
print("")
}
}

结果:

For string length 26:
Method 1 time: 0.00108612
Method 2 time: 0.00085294
Method 3 time: 0.00005889
Method 4 time: 0.00002104
Method 5 time: 0.00000405

For string length 260:
Method 1 time: 0.00117570
Method 2 time: 0.00670648
Method 3 time: 0.00115579
Method 4 time: 0.00110406
Method 5 time: 0.00007111

For string length 2600:
Method 1 time: 0.09964919
Method 2 time: 0.57621503
Method 3 time: 0.09244329
Method 4 time: 0.09166771
Method 5 time: 0.00087011

For string length 26000:
Method 1 time: 9.78054154
Method 2 time: 56.92994779
Method 3 time: 9.02372885
Method 4 time: 9.01480001
Method 5 time: 0.03442019

分析:

  1. 转换为数组的成本很高,尤其是随着数组大小的增长。
  2. 如果您可以保留转换后的 [Character],对其进行索引操作会非常快。 (见方法五)
  3. 方法1、3、4的速度都差不多,根据个人喜好选择。

关于swift - Swift 4 中更简单的字符串切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46247697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com