如何在 Swift 中使用 strstr 在字符串中搜索字符串?我不确定如何使用 UnsafePointer!在 swift 。 strstr 接受两个该类型的参数并返回该类型的值。这是 C 还是 C++?我可以在 Swift 中使用 C 或 C++ 代码,还是可以在 Objective-C 中使用 strstr 并使用桥接 header ?
当我使用这段代码时:
var str1 = "Hello"
var str2 = "ll"
var ptr = strstr(str1, str2)
print(ptr)
print(ptr?.pointee)
我得到这个结果:
Optional(0x00006000039bd762)
Optional(0)
最佳答案
Swift 是一种非常强大的语言。试用它的一些更高级别的功能。 试着把它放在 Playground 上。
var word = "Hello"
print(String(format: "Word: %@", word))
let range = word.range(of: "ll")!
print(String(format: "Lower Bound: %d", range.lowerBound.encodedOffset))
print(String(format: "Upper Bound: %d", range.upperBound.encodedOffset))
word = word.replacingOccurrences(of: "ll", with: "")
print(String(format: "Word after replacement: %@", word))
for (i, c) in word.enumerated() {
print(String(format: "Index: %d, Character: %@", i, String(c)))
}
关于swift - 如何在 Swift 中使用 strstr 在字符串中搜索字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53451015/