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 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.
- 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, a delegate that targets the 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. The type of the delegate is ReplaceEvaluator. It targets a method that accepts a ReplaceEvaluatorArgs 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.
The following example shows how to use the aforementioned overloads. The sample class provides methods, each of which uses a Range.Replace overload:
- Replace1 simply replaces all occurrences of the word "sad" to "bad".
- Replace2 replaces all occurrences of the words "sad" or "mad" to "bad".
- Replace3 uses a replace evaluator method to concatenate occurrences of words "sad" or "bad" with the counter value that is incremented each time the new occurrence is found.
Example RangesReplaceString
Shows how to replace all occurrences of word "sad" to "bad".
[C#]
Document doc = new Document(MyDir + "Document.doc");
doc.Range.Replace("sad", "bad", false, true);
[Visual Basic]
Dim doc As Document = New Document(MyDir & "Document.doc")
doc.Range.Replace("sad", "bad", False, True)
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
doc.getRange().replace("sad", "bad", false, true);
Example RangesReplaceRegex
Shows how to replace all occurrences of words "sad" or "mad" to "bad".
[C#]
Document doc = new Document(MyDir + "Document.doc");
doc.Range.Replace(new Regex("[s|m]ad"), "bad");
[Visual Basic]
Dim doc As Document = New Document(MyDir & "Document.doc")
doc.Range.Replace(New Regex("[s|m]ad"), "bad")
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
doc.getRange().replace(Pattern.compile("[s|m]ad"), "bad");
Example RangesReplaceWithReplaceEvaluator
Shows how to replace with a custom evaluator.
[C#]
public void ReplaceWithEvaluator()
{
Document doc = new Document(MyDir + "Document.doc");
mMatchNumber = 0;
doc.Range.Replace(new Regex("[s|m]ad"), new ReplaceEvaluator(MyReplaceEvaluator), true);
doc.Save(MyDir + "ReplaceWithEvaluator Out.doc");
}
/// <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>
private ReplaceAction MyReplaceEvaluator(object sender, ReplaceEvaluatorArgs e)
{
e.Replacement = e.Match.Value + mMatchNumber.ToString();
mMatchNumber++;
return ReplaceAction.Replace;
}
private int mMatchNumber;
[Visual Basic]
Public Sub ReplaceWithEvaluator()
Dim doc As Document = New Document(MyDir & "Document.doc")
mMatchNumber = 0
doc.Range.Replace(New Regex("[s|m]ad"), New ReplaceEvaluator(AddressOf MyReplaceEvaluator), True)
doc.Save(MyDir & "ReplaceWithEvaluator Out.doc")
End Sub
''' <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>
Private Function MyReplaceEvaluator(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction
e.Replacement = e.Match.Value + mMatchNumber.ToString()
mMatchNumber += 1
Return ReplaceAction.Replace
End Function
Private mMatchNumber As Integer
[Java]
public void ReplaceWithEvaluator() throws Exception
{
Document doc = new Document(getMyDir() + "Document.doc");
mMatchNumber = 0;
doc.getRange().replace(Pattern.compile("[s|m]ad"), new MyReplaceEvaluator(), true);
doc.save(getMyDir() + "ReplaceWithEvaluator Out.doc");
}
/// <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>
public class MyReplaceEvaluator implements ReplaceEvaluator
{
public int replace(Object object, ReplaceEvaluatorArgs e) throws Exception
{
e.setReplacement(e.getMatch().toString() + mMatchNumber);
mMatchNumber++;
return ReplaceAction.REPLACE;
}
}
private int mMatchNumber;