.Replace with matchcase = true not working

I am calling doc.Range.Replace("[Date]", "test", new FindReplaceOptions{MatchCase=true}); and in my document, "[date]" and "[Date]" are both being replaced.

It seems that you are experiencing an issue with the MatchCase option in the Range.Replace method. When you set MatchCase to true, the expectation is that only the exact case of the string will be replaced. However, if both “[date]” and “[Date]” are being replaced despite this setting, it could be due to a few reasons.

Here are some steps you can take to troubleshoot and ensure that case sensitivity works as intended:

  1. Check the Input Strings: Ensure that the string you are searching for (“string”) and the replacement string (“[Date]”) are correctly specified and that there are no leading or trailing spaces.

  2. Verify the FindReplaceOptions: Make sure that the FindReplaceOptions object is correctly instantiated and that MatchCase is indeed set to true before calling the Replace method.

  3. Test with a Simple Example: Create a minimal example where you only replace “[date]” and “[Date]” in a controlled environment. This can help isolate the issue.

Here’s a simple code snippet to illustrate how to use the Range.Replace method with MatchCase:

Document doc = new Document("YourDocument.docx");
FindReplaceOptions options = new FindReplaceOptions
{
    MatchCase = true
};

// This will only replace "[Date]" and not "[date]"
doc.Range.Replace("[Date]", "[NewDate]", options);
doc.Save("UpdatedDocument.docx");

If the issue persists after these checks, it may be beneficial to consult the Aspose documentation or support for further assistance, as there could be specific nuances in your document that affect the behavior of the replace operation.

If you have any additional details or specific scenarios, feel free to share them for more tailored assistance!

@dmerkle1 As I can see the code properly works. Here is the test code:

Document doc = new Document(@"C:\Temp\in.docx");
doc.Range.Replace("[Date]", "test", new FindReplaceOptions { MatchCase = true });
doc.Save(@"C:\Temp\out.docx");

in.docx (12.7 KB)
out.docx (10.0 KB)

As you can see only [Date] values were replaced. [date] remained unchanged.

Was there a bug in 20.3.0? Because it does not work in our version. We are also looping through a number of terms to replace. If I replace [date] first, it does however only replace [date], and then [Date] is replaced in the next iteration.

@dmerkle1 I tested the same code with 20.3 version and the result exactly the same as with 24.7 version. So the problem is not reproducible on my side.
out_20.3.docx (10.1 KB)