Edit this page
Available for: UI for ASP.NET AJAX | UI for ASP.NET MVC | UI for WPF | UI for WinForms

Find and Replace

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.

Replacing can be achieved with the help of the RadFlowDocumentEditor class exposed by the library which works with a specific document.

Replace Text

Finding all instances of a string and replacing it with another can be achieved with the ReplaceText() method of RadFlowDocumentEditor. The editor exposes three methods accepting different parameters.

  • void ReplaceText(string oldText, string newText): Replaces all occurrences of the oldText string with the newText string. Matches the casing of the old text and replaces occurrences within a word as well.

  • void ReplaceText(string oldText, string newText, bool matchCase, bool matchWholeWord): Replaces all occurrences of a string with another string. Accepts Boolean parameters which specify whether the casing should be matched and only whole words should be replaced.

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

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

[C#] Example 1: Replace text

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
editor.ReplaceText("code", "source code", true, 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 2 shows how to apply a red highlight color to all occurrences of the word "alert".

[C#] Example 2: Replace character properties

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

See Also