在 OO Basic 中使用 replaceAll 时,有没有办法计算实际替换的次数?
Is there a way to count the number of actual replaces when using replaceAll in OO Basic?
考虑从 Editing Text Documents OO Wiki:
中搜索和替换特定英国到美国单词的示例
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 次 'colour' 和 1 次 'behaviour',最后获取数字 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
考虑从 Editing Text Documents OO Wiki:
中搜索和替换特定英国到美国单词的示例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 次 'colour' 和 1 次 'behaviour',最后获取数字 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