Find Method


Returns a text range identifying the first occurrence of the given string within the text, starting from a given index.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 22.0.0.0 (22.1.0.0)
Since: 2017

Syntax

C#
public TextRange Find(
	string searchString,
	int startIndex,
	bool matchCase,
	bool matchWholeWord
)
Visual Basic
Public Function Find ( _
	searchString As String, _
	startIndex As Integer, _
	matchCase As Boolean, _
	matchWholeWord As Boolean _
) As TextRange
Visual C++
public:
TextRange^ Find(
	String^ searchString, 
	int startIndex, 
	bool matchCase, 
	bool matchWholeWord
)

Parameters

searchString
Type: System String
The text to search for.
startIndex
Type: System Int32
The start index to search within the text.
matchCase
Type: System Boolean
True if the case must match when searching the formatted text, false to search in a case-insensitive manner.
matchWholeWord
Type: System Boolean
True if the match must be a whole word when searching the formatted text, false otherwise.

Return Value

The text range identified.

Remarks

Returns an empty text range:
  • if the given string cannot be found in the text.
  • if the given start index is beyond the length of the entire text.
The search can be case-sensitive or case-insensitive. The search can be set to match whole words or part of words.

Examples

Copy C#
#region Autodesk.Revit.DB.FormattedText.#ctor(System.String)
public void ReformatText(TextNote textNote, string textToChange)
{
    String plainText = textNote.Text;
    FormattedText formattedText = new FormattedText(plainText);

    TextRange foundRange = formattedText.Find(textToChange, 0, false, true);
    while (foundRange.Length > 0)
    {
        formattedText.SetBoldStatus(foundRange, true);
        foundRange = formattedText.Find(textToChange, foundRange.End, false, true);
    }

    textNote.SetFormattedText(formattedText);
}
#endregion
Copy VB.NET
#Region "Autodesk.Revit.DB.FormattedText.#ctor(System.String)"
        Public Sub ReformatText(textNote As TextNote, textToChange As String)
            Dim plainText As [String] = textNote.Text
            Dim formattedText As New FormattedText(plainText)

            Dim foundRange As TextRange = formattedText.Find(textToChange, 0, False, True)
            While foundRange.Length > 0
                formattedText.SetBoldStatus(foundRange, True)
                foundRange = formattedText.Find(textToChange, foundRange.[End], False, True)
            End While

            textNote.SetFormattedText(formattedText)
        End Sub
#End Region

Exceptions

Exception Condition
Autodesk.Revit.Exceptions ArgumentException searchString is an empty string. -or- searchString contains invalid characters such as a newline character.
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was null
Autodesk.Revit.Exceptions ArgumentOutOfRangeException The given value for startIndex is negative.

See Also

ArchiLabs

Stop fighting Revit automation.

Build repeatable BIM design and documentation workflows with scripts, data, and AI in one place, without wrestling brittle one-off automations.

Try ArchiLabs →