aqString.Find Method

Applies to TestComplete 14.50, last modified on April 22, 2021

Description

Use the Find method to find the first occurrence of a substring in the input string. If the input string holds several occurrences of a substring, the method returns the position of the first matching substring. To search for another occurrence, set a different starting point via the StartPosition parameter.

Declaration

aqString.Find(InputStringSubStringStartPositionCaseSensitive)

InputString [in]    Required    String    
SubString [in]    Required    String    
StartPosition [in]    Optional    Integer Default value: 0   
CaseSensitive [in]    Optional    Boolean Default value: True   
Result Integer

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

InputString

Specifies the string where a substring will be sought for.

SubString

Specifies the sought-for string.

StartPosition

Specifies the initial position of the search. By default (StartPosition=0), the search starts from the beginning of the string.

CaseSensitive

Specifies whether the comparison should be case-sensitive or not.

Result Value

If the substring was found, the method returns the number of the first matching character, otherwise, it returns -1.

Example

The code below searches for the astra substring within the Per aspera ad astra string.

JavaScript, JScript

function StringOccurrenceDemo()
{

  var aString = "Per aspera ad astra";
  var aSubString = "astra";
  var Res = aqString.Find(aString, aSubString);
  if ( Res != -1 )
    Log.Message("Substring '" + aSubString + "' was found in string '" + aString + "' at position " + Res)
  else
    Log.Message("There are no occurrences of '" + aSubString + "' in '" + aString + "'")
}

Python

def StringOccurrenceDemo():
  aString = "Per aspera ad astra"
  aSubString = "astra"
  Res = aqString.Find(aString, aSubString)
  if Res != -1:
    Log.Message("Substring '" + aSubString + "' was found in string '" + aString + "' at position " + str(Res))
  else:
    Log.Message("There are no occurrences of '" + aSubString + "' in '" + aString + "'")

VBScript

Sub StringOccurrenceDemo
  Dim aString, aSubString, Res

  aString = "Per aspera ad astra"
  aSubString = "astra"
  Res = aqString.Find(aString, aSubString)
  If Res <> -1 Then
    Log.Message("Substring '" & aSubString & "' was found in string '" & aString & "' at position " & Res)
  Else
    Log.Message("There are no occurrences of '" & aSubString & "' in '" & aString & "'")
  End If
End Sub

DelphiScript

function StringOccurrenceDemo;
var
  aString, aSubString, Res;
begin

  aString := 'Per aspera ad astra';
  aSubString := 'astra';
  Res := aqString.Find(aString, aSubString);
  if ( Res <> -1 ) then
    Log.Message('Substring ' + aSubString + ''' was found in string ''' + aString + ''' at position ' + VarToStr(Res))
  else
    Log.Message('There are no occurrences of ''' + aSubString + ''' in ''' + aString + '''')
end;

C++Script, C#Script

function StringOccurrenceDemo()
{

  var aString = "Per aspera ad astra";
  var aSubString = "astra";
  var Res = aqString["Find"](aString, aSubString);
  if ( Res != -1 )
    Log["Message"]("Substring '" + aSubString + "' was found in string '" + aString + "' at position " + Res)
  else
    Log["Message"]("There are no occurrences of '" + aSubString + "' in '" + aString + "'")
}

See Also

Working With Strings
SubString Method
Replace Method
Remove Method

Highlight search results