Aspose.words using the tutorial to find and replace text in a document
Aspose.words is an advanced document processing control that enables users to perform various document processing tasks in various applications, including document generation, modification, rendering, printing, without the use of Microsoft Words, Document format conversion, mail merge, and other documents processing. In addition, Aspose.words supports Doc,ooxml,rtf,html,opendocument, PDF, XPS, epub and other formats.
scope of Use: Replace a specific string in the previous range to find or replace because it returns the number of replacements, it is useful to search for strings without replacing them. If a capture or replacement contains one or more special characters: paragraph, cell rupture, partial break, field start, Fields delimiter, field, inline picture, drawing object, footnote string, when an exception occurs.
within a certain range, The workaround provides several overloads. Here are the possibilities they offer :
-
You can specify a string to be replaced, and once replaced, all the strings will be replaced, and the substitution is case-sensitive. Or only independent words will be affected. Note that a word is defined to consist only of alphanumeric characters.
-
You can use a regular expression pattern to find matches and a string, and then replace them. This overload substitution captures the entire match through a regular expression.
-
You can implement the [{{ireplacingcallback}}] interface through the regular expression pattern and an object. This presents a user-defined method that evaluates the replacement in each step, and you can also indicate whether the replacement should be in the forward or backward direction. It is recommended that if you remove a node during the replacement process, the replacement should be performed backwards to avoid any potential problems with removing the node during the replacement process. [ireplacingcallback. replacing] method, It accepts an object that provides a custom alternate data operation [{{Replacingargs}}]. This method should return a [{{replaceaction}}] enumeration value that specifies what happened during the current match replacement operation-whether it should be replaced, skipped, or the entire replacement operation should be terminated.
The following example shows how to use the previously mentioned overload. The sample class provides the use of the Range.replace method:
Example 1 replaces all occurrences of “sad” with “bad”.
Example 2 replaces all occurrences of "sad" or "mad" with "bad".
Example 3 uses the replacement evaluation method to connect the occurrences of the word “sad” or “bad&rdquo, and increments each occurrence of the count value accordingly.
Example 1: Replace one word with another
Replace all occurrences of "sad" with "bad".
C#
Document doc = new document (MyDir + @ "In.docx");
Doc. Range.replace ("Sad", "bad", false, true);
Visual Basic
Dim Doc as New Document (MyDir & "Document.doc")
Doc. Range.replace ("Sad", "bad", False, True)
Example 2: Replace two similar words with one word
Use "bad" to replace all "sad" and "mad".
C#
Document doc = new document (MyDir + "Document.doc");
Doc. Range.replace (New Regex ("[S|m]ad"), "bad");
Visual Basic
Dim Doc as New Document (MyDir & "Document.doc")
Doc. Range.replace (New Regex ("[S|m]ad"), "bad")
Example 3: Using a custom counter
How to replace a custom counter
C#
public void Replacewithevaluator ()
{
document doc = new document (mydir + " Range.ReplaceWithEvaluator.doc "); doc. Range.replace (New regex ("[S|m]ad"), new myreplaceevaluator (), true); doc. Save (mydir + "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;} Public sub replacewithevaluator () dim doc as new document (mydir & "Range.ReplaceWithEvaluator.doc") doc. Range.replace (New regex ("[S|m]ad"), new myreplaceevaluator (), true) doc. Save (mydir & "Range.replacewithevaluator out.doc") end subprivate class myreplaceevaluator implements 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. private function ireplacingcallback_replacing (ByVal e as replacingargs) As ReplaceAction Implements Ireplacingcallback.replacing e.replacement = e. Match.tostring () & mmatchnumber.tostring () Mmatchnumber += 1 return replaceaction.replace End Function Private mMatchNumber As Integerend class
View aspose.words More Latest Tutorials
Aspose.words using the tutorial to find and replace text in a document