Swift 查找字符串中单词的结束索引
Swift find the end index of a word within a string
我有一个字符串。
我在那个字符串里面有一个词
我想基本上摆脱字符串和单词中的所有内容。
但是我想保留字符串中单词之后的所有内容。
这个词和字符串可以是动态的。
这是我目前得到的结果,我发现很难找到在线资源来解决这个问题。
错误:
Cannot convert value of type 'String' to expected argument type 'String.Element' (aka 'Character')
这是我的代码
var bigString = "This is a big string containing the pattern"
let pattern = "containing" //random word that is inside big string
let indexEndOfPattern = bigString.lastIndex(of: pattern) // Error here
let newText = bigString[indexEndOfPattern...]
bigString = newText // bigString should now be " the pattern"
考虑范围和范围,lastIndex(of
期望单个Character
var bigString = "This is a big string containing the pattern"
let pattern = "containing" //random word that is inside big string
if let rangeOfPattern = bigString.range(of: pattern) {
let newText = String(bigString[rangeOfPattern.upperBound...])
bigString = newText // bigString should now be " the pattern"
}
我有一个字符串。 我在那个字符串里面有一个词
我想基本上摆脱字符串和单词中的所有内容。 但是我想保留字符串中单词之后的所有内容。 这个词和字符串可以是动态的。
这是我目前得到的结果,我发现很难找到在线资源来解决这个问题。
错误:
Cannot convert value of type 'String' to expected argument type 'String.Element' (aka 'Character')
这是我的代码
var bigString = "This is a big string containing the pattern"
let pattern = "containing" //random word that is inside big string
let indexEndOfPattern = bigString.lastIndex(of: pattern) // Error here
let newText = bigString[indexEndOfPattern...]
bigString = newText // bigString should now be " the pattern"
考虑范围和范围,lastIndex(of
期望单个Character
var bigString = "This is a big string containing the pattern"
let pattern = "containing" //random word that is inside big string
if let rangeOfPattern = bigString.range(of: pattern) {
let newText = String(bigString[rangeOfPattern.upperBound...])
bigString = newText // bigString should now be " the pattern"
}