我有一个字符串:
"Location: <<LesionLoc>><br>
Quality: <<TESTTEST>><br>"
最终结果应该是:
Location: <span class=\"span_dropdowntext\" keyword=\"LesionLoc\" style=\"color:blue;font-weight:bold;\"contenteditable=\"False\">LesionLoc</span>
Quality: <span class=\"span_dropdowntext\" keyword=\"TESTTEST\" style=\"color:blue;font-weight:bold;\"contenteditable=\"False\"> TESTTEST</span>
如您所见,我所要做的就是:
<<
替换<span class=\"span_dropdowntext\" keyword=\"LesionLoc\" style=\"color:blue;font-weight:bold;\"contenteditable=\"False\">
>>
替换</span>
keyword=/"/"
部分。替换很容易,只需使用string.replacingOcurences然后我认为如果我可以循环遍历所有<<
和>>
,在循环内替换它并将字符串插入关键字部分会更好。
我真的迷失了。有人提到使用索引和子字符串函数来返回新字符串,但我不知道如何处理它。
谢谢。
首先,您必须找到您的关键字(在这种情况下是LesionLoc
和TESTTEST
)。您可以使用带有正则表达式NSRegularExpression
的<<[a-z0-9]+>>
找到它
接下来,用你的<<
替换
<span class=\"span_dropdowntext\" keyword=\"<KEYWORD>\"style=\"color:blue;font-weight:bold;\"contenteditable=\"False\">
和>>
与</span>
。
您可以使用这个非常简单的代码
var string = "Location: <<LesionLoc>><br>\nQuality: <<TESTTEST>><br>"
do {
let regex = try NSRegularExpression(pattern: "<<[a-z0-9]+>>", options: NSRegularExpression.Options.caseInsensitive)
for text in regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.count)).reversed() {
let keyword = (string as NSString).substring(with: text.range).replacingOccurrences(of: "<<", with: "").replacingOccurrences(of: ">>", with: "")
let newString = "<span class=\"span_dropdowntext\" keyword=\"\(keyword)\" style=\"color:blue;font-weight:bold;\"contenteditable=\"False\">\(keyword)</span>"
string = (string as NSString).replacingCharacters(in: text.range, with: newString)
}
print(string)
} catch {
print(error.localizedDescription)
}
private func replaceString(_ str : String)->String{
guard let keyword = findKeyword(str) else{
fatalError()
}
let replacementString = "<span class=\"span_dropdowntext\" keyword=\"\(keyword)\" style=\"color:blue;font-weight:bold;\"contenteditable=\"False\">"
var newString = str.replacingOccurrences(of: ">>", with: "</span>")
newString = newString.replacingOccurrences(of: "<<", with: replacementString)
return newString
}
private func findKeyword(_ s : String)->String?{
var str = Array(s)
let firstKey = "<<"
let secondKey = ">>"
var startingIndex : Int = 0
var movingIndex : Int = 0
while movingIndex < str.count + 1 - secondKey.count && startingIndex < str.count + 1 - firstKey.count{
if String(str[startingIndex..<startingIndex + firstKey.count]) != firstKey{
startingIndex += 1
}else{
if movingIndex < startingIndex + firstKey.count{
movingIndex = startingIndex + firstKey.count
}else{
if String(str[movingIndex..<movingIndex + secondKey.count]) != secondKey{
movingIndex += 1
}else{
return String(str[startingIndex+firstKey.count..<movingIndex])
}
}
}
}
return nil
}
现在,您可以使用replaceString函数来完成工作
let string = "Location: <<LesionLoc>><br>"
print(replaceString(string))
你的最终结果将是
Location: <span class="span_dropdowntext" keyword="LesionLoc" style="color:blue;font-weight:bold;"contenteditable="False">LesionLoc</span><br>
结果字符串的末尾有一个“br”。我不知道你是否想删除它..但如果你这样做,你应该知道如何。