如何使用Swift中的strstr搜索字符串中的字符串?我不知道如何使用UnsafePointer!在斯威夫特。strstr接受该类型的两个参数,并返回该类型的值。这是C还是C++?我可以在Swift中使用C或C++代码,或者在Objective中使用strstr并使用桥接头吗?
当我使用这个代码时:
var str1 = "Hello"
var str2 = "ll"
var ptr = strstr(str1, str2)
print(ptr)
print(ptr?.pointee)我得到了这个结果:
可选(0x00006000039bd 762) 任择(0)
发布于 2018-11-23 17:55:45
斯威夫特是一种非常强大的语言。尝试一些更高级的特性。试着把这个放在操场上。
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)))
}https://stackoverflow.com/questions/53451015
复制相似问题