Swift:获取字符串中子字符串开始的索引 [英] Swift: get index of start of a substring in a string
本文介绍了Swift:获取字符串中子字符串开始的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在字符串中搜索子字符串开头的索引?
Is there a way to search a string for the index of the start of a substring?
例如,"hello".indexOf("el")
将返回1.谢谢
推荐答案
您可以使用rangeOfString
函数来查找并返回给定字符串在给定范围内首次出现的范围,如下所示:
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
}
希望这对您有所帮助.
这篇关于Swift:获取字符串中子字符串开始的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文