Swift 4 某字符的字符串范围
Swift 4 string range of a certain character
我试图在字符串中创建一个函数,从提供的字符串中删除一些字符。
例如我有
"Boll %b (Teeth Alligator (13,8,8,5,5,3,n),20,2,ma)"
我想将其编辑为
"Boll %b"
表示删除"("
和")"
之间的所有内容
所以我做了这个功能
func deleteInnerString(with string: String, from firstCharacter: String, to secondCharacter: String) -> String {
guard let leftIndex = (string.range(of: firstCharacter)?.lowerBound),
let rightIndex = string.range(of: secondCharacter)?.lowerBound else {
return string
}
let remainingString = String(string.prefix(upTo: leftIndex) + string.suffix(from: string.index(after: rightIndex)))
return remainingString
}
但在这种情况下输出字符串是
"Boll %b ,20,2,ma)"
问题是,如何扫描字符串以找到最后一个右括号范围??
String.range
函数可以提供额外的选项,因此您可以将 String.CompareOptions.backwards
参数传递给它以获取最后一个实例。
func deleteInnerString(with string: String, from firstCharacter: String, to secondCharacter: String) -> String {
guard let leftIndex = (string.range(of: firstCharacter)?.lowerBound),
let rightIndex = string.range(of: secondCharacter, options: .backwards)?.lowerBound else {
return string
}
let remainingString = String(string.prefix(upTo: leftIndex) + string.suffix(from: string.index(after: rightIndex)))
return remainingString
}
您必须在字符串的 range
函数中为 String.CompareOptions
使用 .backwards
选项。如下图
func deleteString(with string: String, from firstCharacter: String, to secondCharacter: String) -> String {
guard let leftIndex = (string.range(of: firstCharacter)?.lowerBound),
let rightIndex = string.range(of: secondCharacter, options: .backwards)?.lowerBound else {
return string
}
let remainingString = String(string.prefix(upTo: leftIndex) + string.suffix(from: string.index(after: rightIndex)))
return remainingString.trimmingCharacters(in: .whitespaces)
}
请注意,您的策略不适用于非嵌套括号。
例如这个输入
a(b)c(d)e
用你的方法会产生这个输出
ae
哪个恕我直言不正确。
解决方案
此解决方案将适用于 嵌套 和 非嵌套 括号
let text = "a(b)c(d)e"
var numOfNestedParentesis = 0
var indexesToRemove:Set<Int> = []
for (index, char) in text.enumerated() {
if char == "(" {
numOfNestedParentesis += 1
}
if numOfNestedParentesis > 0 {
indexesToRemove.insert(index)
}
if char == ")" {
numOfNestedParentesis -= 1
}
}
let result = String(text.enumerated()
.filter { !indexesToRemove.contains([=12=].offset) }
.map { [=12=].element } )
测试
print(result) // ace
我试图在字符串中创建一个函数,从提供的字符串中删除一些字符。
例如我有
"Boll %b (Teeth Alligator (13,8,8,5,5,3,n),20,2,ma)"
我想将其编辑为
"Boll %b"
表示删除"("
和")"
所以我做了这个功能
func deleteInnerString(with string: String, from firstCharacter: String, to secondCharacter: String) -> String {
guard let leftIndex = (string.range(of: firstCharacter)?.lowerBound),
let rightIndex = string.range(of: secondCharacter)?.lowerBound else {
return string
}
let remainingString = String(string.prefix(upTo: leftIndex) + string.suffix(from: string.index(after: rightIndex)))
return remainingString
}
但在这种情况下输出字符串是
"Boll %b ,20,2,ma)"
问题是,如何扫描字符串以找到最后一个右括号范围??
String.range
函数可以提供额外的选项,因此您可以将 String.CompareOptions.backwards
参数传递给它以获取最后一个实例。
func deleteInnerString(with string: String, from firstCharacter: String, to secondCharacter: String) -> String {
guard let leftIndex = (string.range(of: firstCharacter)?.lowerBound),
let rightIndex = string.range(of: secondCharacter, options: .backwards)?.lowerBound else {
return string
}
let remainingString = String(string.prefix(upTo: leftIndex) + string.suffix(from: string.index(after: rightIndex)))
return remainingString
}
您必须在字符串的 range
函数中为 String.CompareOptions
使用 .backwards
选项。如下图
func deleteString(with string: String, from firstCharacter: String, to secondCharacter: String) -> String {
guard let leftIndex = (string.range(of: firstCharacter)?.lowerBound),
let rightIndex = string.range(of: secondCharacter, options: .backwards)?.lowerBound else {
return string
}
let remainingString = String(string.prefix(upTo: leftIndex) + string.suffix(from: string.index(after: rightIndex)))
return remainingString.trimmingCharacters(in: .whitespaces)
}
请注意,您的策略不适用于非嵌套括号。
例如这个输入
a(b)c(d)e
用你的方法会产生这个输出
ae
哪个恕我直言不正确。
解决方案
此解决方案将适用于 嵌套 和 非嵌套 括号
let text = "a(b)c(d)e"
var numOfNestedParentesis = 0
var indexesToRemove:Set<Int> = []
for (index, char) in text.enumerated() {
if char == "(" {
numOfNestedParentesis += 1
}
if numOfNestedParentesis > 0 {
indexesToRemove.insert(index)
}
if char == ")" {
numOfNestedParentesis -= 1
}
}
let result = String(text.enumerated()
.filter { !indexesToRemove.contains([=12=].offset) }
.map { [=12=].element } )
测试
print(result) // ace