0

Swift String indexOf 查找字符串中某一个字符串的位置怎样写?

stephanie created at6 years ago view count: 301

"hello wold, hello code" 想要查找wold的index位置怎样写的

report
回复
0
var mystring = "hello wold, hello code";
var data : Data = mystring.data(using: .utf8)!;
if let range = mystring.range(of: "wold") {
    let startPos = mystring.distance(from: mystring.startIndex, to: range.lowerBound)
    data = data.subdata(in: Range(startPos..<data.endIndex));
    print(String(data:data, encoding:.ascii)!)
}
6 years ago 回复