Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Xamarin | UI for WinUI | UI for ASP.NET Core

New to Telerik Document Processing? Download free 30-day trial

Find and Replace Text and Style

RadWordsProcessing gives you the ability to search for a string in a RadFlowDocument instance and replace all matches. The library also allows you to replace the styling of the matches alone.

You can search and replace text or styling using RadFlowDocumentEditor. This article lists the available methods and describes how you can use them. This feature is available since R2 2021 release version.

Find Text

RadFlowDocumentEditor exposes the FindAll() method to enable you to find all instances of a string. You can choose between the following overloads:

  • FindAll(string text, bool matchCase=true, bool matchWholeWord=false): Finds all occurrences of the specified string. The last two parameters are optional. If these parameters are not set, the default values are true for matchCase and false for matchWholeWord.

  • FindAll(Regex regex): Finds all matches of the passed Regex.

Both methods return a collection of FindResult instances, which in turn expose the following properties:

  • Runs: Gets a collection of Runs that contains the searched text.

  • RelativeStartIndex: Gets the index of the first character in the searched text inside the first Run.

  • RelativeEndIndex: Gets the index of the last character in the searched text inside the last Run.

  • FullMatchText: Gets the matched text.

Example 1 shows how to create a RadFlowDocumentEditor instance and use it to find all matches of the word "code".

Example 1: Find text

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); // document is an instance of the RadFlowDocument class 
ReadOnlyCollection<FindResult> findResults = editor.FindAll("code", matchCase: true, matchWholeWord: true); 

Replace Text

To find all instances of a string and replace it with another one, you can use the ReplaceText() method of RadFlowDocumentEditor. The method features two overloads accepting different parameters:

  • void ReplaceText(string oldText, string newText, bool matchCase=true, bool matchWholeWord=false): Replaces all occurrences of a string with another string. The last two parameters are optional. If these parameters are not set, the default values are true for matchCase and false for matchWholeWord.

  • void ReplaceText(Regex regex, string newText): Replaces all matches of the specified Regex with the new text.

Example 2 shows how to create a RadFlowDocumentEditor instance and use it to replace all matches of the word "code" with the phrase "source code".

Example 2: Replace text

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); // document is an instance of the RadFlowDocument class 
editor.ReplaceText("code", "source code", matchCase: true, matchWholeWord: true); 

Replace Styling

RadFlowDocumentEditor gives you the ability to format all occurrences of a string in a document. This can be achieved by using one of the overloads of the ReplaceStyling() method. The overloads accept an Action delegate that allows modifying the CharacterProperties of the matches.

  • void ReplaceStyling(string searchedText, Action<CharacterProperties> replacePropertiesAction): Applies the character property changes from the Action delegate to all matches with the casing of the string.

  • void ReplaceStyling(string searchedText, bool matchCase, bool matchWholeWord, Action<CharacterProperties> replacePropertiesAction): Applies the character property changes from the Action delegate to all matches. Accepts Boolean parameters which specify whether the casing should be matched and only whole words should be styled.

  • void ReplaceStyling(Regex regex, Action<CharacterProperties> replacePropertiesAction): Applies the character property changes from the Action delegate to all matches of the Regex.

Example 3 shows how to apply a red highlight color to all occurrences of the word "alert".

Example 3: Replace character properties

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.ReplaceStyling("text", new Action<CharacterProperties>((properties) => 
{ 
    properties.HighlightColor.LocalValue = Colors.Red; 
})); 

See Also

In this article
Not finding the help you need? Improve this article