在OO Basic中使用replaceAll时,有没有办法计算实际替换的数量?



考虑从编辑文本文档OO Wiki中搜索和替换特定uk-to-us单词的示例:

Dim I As Long
Dim Doc As Object
Dim Replace As Object
Dim BritishWords(5) As String
Dim USWords(5) As String
BritishWords() = Array("colour", "neighbour", "centre", "behaviour", _
"metre", "through")
USWords() = Array("color", "neighbor", "center", "behavior", _
"meter", "thru")
Doc = ThisComponent
Replace = Doc.createReplaceDescriptor
For I = 0 To 5
Replace.SearchString = BritishWords(I)
Replace.ReplaceString = USWords(I)
Doc.replaceAll(Replace)
Next I

问题:有没有办法获得已进行的实际更换计数?(如有)我不介意每个术语的单独计数,但只是全局 - 即,例如,如果原始文本包含 2 次"颜色"和 1 次出现"行为",最终得到数字 3(目的:通过MsgBox将此数字作为信息报告给用户)。

如 https://www.openoffice.org/api/docs/common/ref/com/sun/star/util/XReplaceable.html 处的示例所示,返回找到的数字。

Dim TotalFound As Long
TotalFound = 0
...
TotalFound = TotalFound + Doc.replaceAll(Replace)
Next I
MsgBox "Replaced " & TotalFound & " occurrences"

结果:Replaced 3 occurrences

最新更新