Rich Text Editor / Working With Text Documents / Finding And Replacing Text
In This Topic
    Finding And Replacing Text
    In This Topic

    It is a common requirement to search for text fragments inside text documents as well to replace such fragments with different text. In Nevron Text this can be achieved programmatically.

    Finding Text

    The control supports two ways to search for text fragments - iterative and exhaustive. The first approach exposes an iterator like method that allows you to loop trough all occurrences of a text fragment - for example:

    Loading a Text Document From Stream
    Copy Code
    // create some dummy content
    NSection section = new NSection();
    
    string text = string.Empty;
    
    for (int i = 0; i < 3; i++)
    {
    text += "The quick brown fox jumps over the lazy dog.";
    }
    
    NParagraph paragraph = new NParagraph(text);
    section.Blocks.Add(paragraph);
    
    m_RichText.Content.Sections.Clear();
    m_RichText.Content.Sections.Add(section);
    
    // init find settings
    NFindSettings settings = new NFindSettings();
    settings.FindWhat = "dog";
    settings.SearchDirection = ENSearchDirection.Forward;
    
    // loop through all occurances of "dog"
    NRangeI textRange = NRangeI.Zero;
    
    while (m_RichText.FindNext(settings, ref textRange))
    {
    m_RichText.EditingRoot.Selection.SelectRange(textRange);
    m_RichText.EditingRoot.Selection.SetHighlightFillToSelectedInlines(new NColorFill(ENNamedColor.Red));
    }
    
    Searching is performed from the current caret position in the direction specified in text settings.

    The following table lists the options in the find settings object:

    Property Type Description
    FindWhat string Gets or sets the text to search for.
    SearchDirection ENSearchDirection Gets or sets the search direction. Available options are ENSearchDirection.Forward or ENSearchDirection.Backward.
    Normalization ENNormalization Gets or sets the type of normalization to apply to text.
    MatchCase bool Gets or sets whether search is case sensitive.
    WholeWordsOnly bool Whether only whole words will be matched.

    Note that the Normalization property accepts values from the ENNormalization enum the following table lists the avaialable options:

    ENNormalization Description
    None No normalization is applied.
    Compatible Use Unicode compatible normalization (NFKC). Compatibility equivalence is a weaker equivalence between characters or sequences of characters that represent the same abstract character, but may have a different visual appearance or behavior.
    Canonical Use Unicode canonical normalization (NFC). Canonical equivalence is a fundamental equivalency between characters or sequences of characters that represent the same abstract character, and when correctly displayed should always have the same visual appearance and behavior.

    You can also use the FindAll method which returns a list of all ranges that contain the search string:

    Loading a Text Document From Stream
    Copy Code
    // init find settings
    NFindSettings settings = new NFindSettings();
    settings.FindWhat = "dog";
    settings.SearchDirection = ENSearchDirection.Forward;
    
    // find all occurances of "dog"
    NList<NRangeI> textRanges = m_RichText.FindAll(settings);
    
    for (int i = 0; i < textRanges.Count; i++)
    {
    m_RichText.EditingRoot.Selection.SelectRange(textRanges[i]);
    m_RichText.EditingRoot.Selection.SetHighlightFillToSelectedInlines(new NColorFill(ENNamedColor.Red));
    }
    

    Replacing Text

    The previous code examples selected a text and then applied some filling on that text. You can use the InsertText function of the selection to replace the currently selected text with something else - for example, the following code replaces all occurrences of "dog" with "bulldog":

    Loading a Text Document From Stream
    Copy Code
    // init find settings
    NFindSettings settings = new NFindSettings();
    settings.FindWhat = "dog";
    settings.SearchDirection = ENSearchDirection.Forward;
    
    // find all occurances of "dog"
    NRangeI textRange = NRangeI.Zero;
    
    while (m_RichText.FindNext(settings, ref textRange))
    {
    // replace dog with cat
    m_RichText.EditingRoot.Selection.SelectRange(textRange);
    m_RichText.EditingRoot.Selection.InsertText("bulldog");
    }
    
    You cannot use FindAll in this case as the word "bulldog" contains more characters than "dog". This means that once you replace the word "dog" with "bulldog" you need to shift all subsequent ranges.
    See Also