Finding the number of occurrences of a string in a document

I want to find the number of occurrences of a given string, e.g. "<<Signer- " in a Word document. I see from the documentation that I can use “Range.Replace”, but this seems a little kluge to be modifying the document when it should not be necessary to actually modify the document…
Is there a better way to to find the number of occurrences of a given string in a document? If not, could you provide me a little code smippet that returns the number of occurrences of a string in a document without changing the actually contents of the document?
I did find this post https://forum.aspose.com/t/107960, but the snippet in it is out of date.
TIA, Mark

Hello
Thanks for your inquiry. You can use the Range.Replace method to achieve this. In your case you would want to use the method overload that takes simple strings and matches them, for example:

int count = doc.Range.Replace("<<Signer-", "<<Signer-", false, false);

The number of occurances of the string “<<Signer-” in the document is stored in count.
Since you only want to find and not replace these values the same string needs to be passed to the second parameter, this just means the original is replaced with the same text so in effect, nothing happens.
The last two paratmers define if the search is case sensitivve and only matches whole words.
If you have any further queries, please feel free to ask.
Best regards,

Thanks for your quick, clear response… I do appreciate it!
Mark

A post was split to a new topic: Range.Replace does not return correct count