Find and Replace

Use Range.Replace to find or replace a particular string within the current range. It returns the number of replacements made, so it is useful for searching strings without replace. An exception is thrown if a captured or replacement string contains one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, footnote.

The Range.Replace method provides several overloads. Here are the possibilities they provide:
  • You can specify a string to be replaced, a string that will replace all its occurrences, whether the replacement is case-sensitive, and whether only stand-alone words will be affected. Note that a word is defined as being made up of only alpha-numeric characters. If replace is executed with only whole words only being matched and the input string happens to contain symbols, then no phrases will be found.
  • You can pass a regular expression pattern used to find matches and a string that will replace them. This overload replaces the whole match captured by the regular expression.
  • You can pass a regular expression pattern, and an object that implements the IReplacingCallback interface. This sets out a user-defined method, which evaluates replacement at each step, you can also indicate whether the replacement should be done in a forward or backward direction. It is recommended if you are removing nodes during replacement then the replacement should be executed backwards to avoid any potential issues that may arise when removing nodes during the replacement process. A class implementing the IReplacingCallback interface will define a method IReplacingCallback.Replacing that accepts a ReplacingArgs object providing data for a custom replace operation. The method should return a ReplaceAction enumeration value that specifies what happens to the current match during a replace operation - whether it should be replaced, skipped, or the whole replace operation should be terminated.

Example 1: Replace One Word with Another

Document doc = new Document("Find and Replace.doc");
doc.Range.Replace("sad", "bad", false, true);

Example 2: Replace Two Similar Words with One Other

Document doc = new Document("Find and Replace.doc");
doc.Range.Replace(new Regex("[s|m]ad"), "bad");

Example 3: Use a Custom Evaluator

 public void ReplaceWithEvaluator()
        {
           
            Document doc = new Document("Range.ReplaceWithEvaluator.doc");
            doc.Range.Replace(new Regex("[s|m]ad"), new MyReplaceEvaluator(), true);
            doc.Save("Range.ReplaceWithEvaluator Out.doc");
        }

        private class MyReplaceEvaluator : IReplacingCallback
        {
            /// <summary>
            /// 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.
            /// </summary>
            ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
            {
                e.Replacement = e.Match.ToString() + mMatchNumber.ToString();
                mMatchNumber++;
                return ReplaceAction.Replace;
            }

            private int mMatchNumber;
        }
 



Download

Last edited Mar 3, 2014 at 5:15 AM by asposemarketplace, version 3