0
我寫了一個函數,搜索字符串中的給定的標籤,並刪除所有這些標籤及其內容,除了第一個:高效代碼:刪除字符串變量除了第一個
Sub Main()
Dim fileAsString = "<div>myFirstDiv</div>" +
"<Div></dIV>" +
"<city>NY</city>" +
"<city></city>" +
"<div></div>" +
"<span></span>"
' Removes these tags and their content from fileAsString, except the
' first appearance
Dim forbiddenNodeslist As New List(Of String)
forbiddenNodeslist.Add("div")
forbiddenNodeslist.Add("city")
' Run all over the forbidden tags
For Each node In forbiddenNodeslist
Dim re = New Regex("<" + node + "[^>]*>(.*?)</" + node + ">", RegexOptions.IgnoreCase)
Dim matches = re.Matches(fileAsString)
Dim matchesCount = matches.Count - 1
' Count the characters that were replaced by empty string, in order
' to update the start index of the other matches
Dim removedCharacters = 0
' Run all over the matches, except the first one
For index = 1 To matches.Count - 1
Dim match = matches(index)
' set start index and length in order to replace it by empty string
Dim startIndex = match.Index - removedCharacters
Dim matchCharactersCount = match.Length
' Update the number of characters that will be removed
removedCharacters = matchCharactersCount
' Remove it from the string
fileAsString = fileAsString.Remove(startIndex, matchCharactersCount)
Next
Next
end sub
但效率不高的原因我搜索匹配(字符串的第一個循環),然後一次又一次地循環,以便用空字符串替換它。
我該如何提高效率?
任何幫助表示讚賞!
是否有一個原因,你正在存儲removedCharacters和刪除標籤的位置?如果沒有,這只是額外的開銷。循環訪問有問題的標籤列表,刪除並使用單個語句刪除/替換所有的事件。 http://stackoverflow.com/questions/6025560/how-to-ignore-case-in-string-replace – mjw
是的,我存儲它,因爲當我刪除一些字符串,下一場比賽的開始索引將需要更新。例如:「
」,第一個div出現在索引0,第二個在11,第三個在22. 當我刪除第二個div時,第三個div將位於索引11而不是22. –您可以反向整個字符串,然後刪除除LAST之外的所有字符,然後再次反轉以獲得相同的結果。 –