iOS Swift操纵/解析字符串
问题描述:
我不确定如何在iOS Swift中执行此操作
I'm not sure how to do this in iOS Swift
let test = "fnfsjflsjlkdkfj?v=904kg4"
// search test for ?v= and store everything after ?v= into a new string
let newString = "904kg4"
我想要一切之后?v =成新的字符串,这可能吗?如果是这样,我怎么能完成这个
I want everything after ?v= into a new string, is this possible? if so, how can I accomplish this
答
使用 rangeOfString
来查找范围?v =
然后使用 substringFromIndex
来获取字符串的其余部分:
Use rangeOfString
to find the range of ?v=
and then use substringFromIndex
to get the rest of the string:
let test = "fnfsjflsjlkdkfj?v=904kg4"
if let range = test.rangeOfString("?v=") {
let newString = test.substringFromIndex(range.endIndex)
}