Version

Find / Replace (xamSyntaxEditor)

Topic Overview

Purpose

This topic explains the TextDocument’s Find, Replace and ReplaceAll operations.

Note
Note

Find and FindAll operations (without replace) are performed on a snapshot (refer to the Working with Snapshots topic for more information).

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

In this topic, you will find information to help you better understand the xamSyntaxEditor’s functions.

This topic provides you with systematic instructions designed to help you get the xamSyntaxEditor up and running as quickly as possible.

This topic covers the text editing capabilities of the xamSyntaxEditor control from both the developer and user’s perspective.

Introduction

Introduction to the Find and Replace methods

The xamSyntaxEditor’s TextDocument supports searching and replacing text via several powerful methods and their overloads. Specifying search criteria such as case, whole words, search direction or even regular expressions will all help to narrow the search results. The TextSearchCriteria class encapsulated all these parameters, passed as an argument to the Find and Replace methods.

All of the methods listed below return TextSearchResultInfo objects, which can be used to obtain the search criteria used and a list of TextSearchResult objects for each replacement that was made.

In some cases you may need to provide a start offset for the search (i.e., the offset from the beginning of the document), which can be obtained by converting the xamSyntaxEditor Caret’s TextLocation property using the OffsetFromLocation method of the CurrentSnapshot.

Note
Note

Additional information on the TextDocument’s snapshots is available in the Working with Snapshots topic.

Find and Replace Summary

Find and replace summary chart

The following table lists the FindReplace methods. Additional details are available after the table.

Operation Details

Replace one occurrence of the TextSearchCriteria by starting the search from the specified start offset (continue search after the end of the file is optionally available).

Replace one occurrence of the TextSearchCriteria by searching in the specified text span (continue search after the text span is optionally available).

Replace all occurrences of the TextSearchCriteria in the whole document or optionally a span within the document.

FindReplace (by specifying start only)

Overview

Using the criteria specified in the TextSearchCriteria argument the FindReplace method searches the text. The search starts at the position specified in the startOffset argument and if found the text is replaced with the text specified in the newText argument. If the search should continue after the end of the document, set the wrapIfNotFound argument to true .

Method usage

In order to: Invoke this method: And set the following arguments:

Find and replace a single occurrence of the specified text or regular expression

  • newText

  • criteria

  • startOffset

  • wrapIfNotFound (optional)

Example

In C#:

TextSearchCriteria tsc = new TextSearchCriteria(
    "text-to-find", // text to search for
    false, // whole word only
    false, // is case sensitive
    false // search backwards
);
// obtain current caret location and get the offset from it
TextLocation tl = this.xamSyntaxEditor1.Caret.TextLocation;
int offset = this.xamSyntaxEditor1.Document.CurrentSnapshot.OffsetFromLocation(tl);
// find and replace one occurrence
TextSearchResultInfo tsri = this.xamSyntaxEditor1.Document.FindReplace(
    "text-for-replace", // new text to replace with
    tsc, // search criteria
    offset, // start offset
    false // wrap if not found
);
if (tsri.Results != null && tsri.Results.Count > 0)
{
    TextSearchResult tsr = tsri.Results[0];
    TextLocation tl2 = tsr.SpanReplaced.Value.EndLocation;
    // the end location of the "SpanReplaced" may be
    // used for a start offset for the next search
}

In Visual Basic:

' text to search for
' whole word only
' is case sensitive
' search backwards
Dim tsc As New TextSearchCriteria("text-to-find", False, False, False)
' obtain current caret location and get the offset from it
Dim tl As TextLocation = _
    Me.xamSyntaxEditor1.Caret.TextLocation
Dim offset As Integer =  _
    Me.xamSyntaxEditor1.Document.CurrentSnapshot.OffsetFromLocation(tl)
' find and replace one occurrence
' new text to replace with
' search criteria
' start offset
' wrap if not found
Dim tsri As TextSearchResultInfo = _
    Me.xamSyntaxEditor1.Document.FindReplace("text-for-replace", tsc, offset, False)
If tsri.Results IsNot Nothing AndAlso tsri.Results.Count > 0 Then
    Dim tsr As TextSearchResult = tsri.Results(0)
    Dim tl2 As TextLocation = tsr.SpanReplaced.Value.EndLocation
    ' the end location of the "SpanReplaced" may be
    ' used for a start offset for the next search
End If

FindReplace (by specifying text span)

Overview

Using the criteria specified in the TextSearchCriteria argument the FindReplace method searches the text. The search starts at the position specified in the startOffset if it intersects with the text span defined by the spanToSearch argument and if found replaces the text with the text specified in the newText argument.

Method usage

In order to: Invoke this method: And set the following arguments:

Find and replace a single occurrence of the specified text or regular expression

  • newText

  • criteria

  • spanToSearch

  • startOffset

  • wrapIfNotFound (optional)

Example

In C#:

TextSearchCriteria tsc = new TextSearchCriteria(
    "text-to-find", // text to search for
    false, // whole word only
    false, // is case sensitive
    false // search backwards
);
// obtain current caret location and get the offset from it
TextLocation tl = this.xamSyntaxEditor1.Caret.TextLocation;
int offset = this.xamSyntaxEditor1.Document.CurrentSnapshot.OffsetFromLocation(tl);
// create a test span, which begins at the current
// caret location and extends 200 characters beyond
TextSpan ts = new TextSpan(offset, 200);
// find and replace one occurence
this.xamSyntaxEditor1.Document.FindReplace(
    "text-for-replace", // new text to replace with
    tsc, // search criteria
    ts, // the text span where to search
    offset, // start offset
    false // wrap if not found
);

In Visual Basic:

' text to search for
' whole word only
' is case sensitive
' search backwards
Dim tsc As New TextSearchCriteria("text-to-find", False, False, False)
' obtain current caret location and get the offset from it
Dim tl As TextLocation = _
    Me.xamSyntaxEditor1.Caret.TextLocation
Dim offset As Integer =  _
    Me.xamSyntaxEditor1.Document.CurrentSnapshot.OffsetFromLocation(tl)
‘ create a test span, which begins at the current
‘ caret location and extends 200 characters beyond
Dim ts As New TextSpan(offset, 200)
‘ find and replace one occurrence
‘ new text to replace with
‘ search criteria
‘ the text span where to search
‘ start offset
‘ wrap if not found
Me.xamSyntaxEditor1.Document.FindReplace("text-for-replace", tsc, ts, offset, False)

FindReplaceAll

Overview

Using the criteria specified in the TextSearchCriteria argument the FindReplaceAll method searches the text. The search will spread over the whole document or over the text span, if specified. If found the text is replaced with the text specified in the replacementText argument.

Method usage

In order to: Invoke this method: And set the following arguments:

Find and replace all occurrences of the specified text or regular expression

  • criteria

  • replacementText

  • spanToSearch (optional)

Example

In C#:

Example
TextSearchCriteria tsc = new TextSearchCriteria(
    "text-to-find", // text to search for
    false, // whole word only
    false, // is case sensitive
    false // search backwards
);
// find and replace one occurrence
this.xamSyntaxEditor1.Document.FindReplaceAll(
    tsc, // search criteria
    "text-for-replace" // new text to replace with
);

In Visual Basic:

' text to search for
' whole word only
' is case sensitive
' search backwards
Dim tsc As New TextSearchCriteria("text-to-find", False, False, False)
' find and replace one occurrence
' search criteria
' new text to replace with
Me.xamSyntaxEditor1.Document.FindReplaceAll(tsc, "text-for-replace")

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic describes features provided by the TextDocumentSnapshot and the TextDocumentSnapshotScanner classes.