Word Replace

Hi
I have a source document, which has a string “Hello”. This string is present in the document 5 times.
Eg: In text “Hi today the day is very good. The climate is very good”. Here ‘the’ is present twice.
Similarly, in my document “Hello” is coming 5 times.
Now I want to replace it dynamically, such that the output document has “Hello_0”, “Hello_1”, “Hello_2”, and so on.
Can you please help me?
Thanking you in anticipation.

Hi there,

Thanks for your inquiry.

Yes, you can achieve your requirement by implementing IReplacingCallback interface. Please read following documentation links for your kind reference.
https://docs.aspose.com/words/java/find-and-replace/

Please use the following code snippet to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

private class MyReplaceEvaluator: IReplacingCallback
{
    ///
    /// This is called during a replace operation each time a match is found.
    /// This method appends a number to the match string and returns it as a replacement string.
    ///
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        e.Replacement = e.Match.ToString() + mMatchNumber.ToString();
        mMatchNumber++;
        return ReplaceAction.Replace;
    }
    public int mMatchNumber;
}
Document doc = new Document(MyDir + "in.docx");
doc.Range.Replace(new Regex("Hello"), new MyReplaceEvaluator(), true);
doc.Save(MyDir + "Out.docx");