Swift: get index of start of a substring in a string [duplicate]

拈花ヽ惹草 提交于 2019-12-13 00:22:51

问题


Is there a way to search a string for the index of the start of a substring?

Like, "hello".indexOf("el") would return 1. Thanks


回答1:


You can reach using the rangeOfString function that finds and returns the range of the first occurrence of a given string within a given range like in the following way:

var text = "Hello Victor Sigler"

if let indexOf = text.rangeOfString("Victor")?.startIndex {
   println(indexOf) //6
}

I hope this help you.



来源:https://stackoverflow.com/questions/32105260/swift-get-index-of-start-of-a-substring-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!