aqString.FindLast Method

Applies to TestComplete 15.69, last modified on November 13, 2024

Description

Use the FindLast method to find a substring in the input string. If the input string holds several occurrences of the substring, the method returns the position of the last matching substring.

Declaration

aqString.FindLast(InputString, SubString, CaseSensitive)

InputString [in]    Required    String    
SubString [in]    Required    String    
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.

CaseSensitive

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

Result Value

If the substring was found, the method returns the zero-based index of the last matching substring. Otherwise, the method returns -1.

Example

The code below searches for the last occurrence of the 'lex' substring within the 'Dura lex, sed lex' string.

JavaScript, JScript

function StringOccurrenceDemo()
{

  var aString, aSubString, Res;
  aString = "Dura lex, sed lex";
  aSubString = "lex";
  Res = aqString.FindLast(aString,aSubString);
  if (Res != -1)
    Log.Message("The last occurrence of the substring '" + aSubString + "' within the string '" + aString + "' is at position " + Res)
  else
           Log.Message("There are no occurrences of the substring '" + aSubString + "' in the '" + aString + "'")

}

// Result:
// The last occurrence of the 'lex' substring within the 'Dura lex, sed lex' string is at position 14

Python

def StringOccurrenceDemo():
  aString = "Dura lex, sed lex"
  aSubString = "lex"
  Res = aqString.FindLast(aString,aSubString)
  if Res != -1:
    Log.Message("The last occurrence of the substring '" + aSubString + "' within the string '" + aString + "' is at position " + str(Res))
  else:
    Log.Message("There are no occurrences of the substring '" + aSubString + "' in the '" + aString + "'")

# Result:
# The last occurrence of the 'lex' substring within the 'Dura lex, sed lex' string is at position 14

VBScript

Sub StringOccurrenceDemo
Dim aString, aSubString, Res

  aString = "Dura lex, sed lex"
  aSubString = "lex"
  Res = aqString.FindLast(aString,aSubString)
  If Res <> -1 then
    Log.Message("The last occurrence of the '" & aSubString & "' substring within the '" & aString & "' string is at position " & Res)
    Else
    Log.Message("There are no occurrences of the '" & aSubString & "' substring in '" & aString + "'")
  End If
End Sub

' Result:
' The last occurrence of the 'lex'substring within the string 'Dura lex, sed lex' is at position 14

DelphiScript

procedure StringOccurrenceDemo;
var aString, aSubString, Res;
begin

  aString := 'Dura lex, sed lex';
  aSubString := 'lex';
  Res := aqString.FindLast(aString,aSubString);
  if Res <> -1 then
           Log.Message('The last occurrence of the ''' + aSubString + ''' substring within the ''' + aString + ''' string is at position ' + IntToStr(Res))
  else
           Log.Message('There are no occurrences of the ''' + aSubString + ''' substring in ''' + aString + ' ''')
end;

// Result:
// The last occurrence of the 'lex' substring within the 'Dura lex, sed lex' string is at position 14

C++Script, C#Script

function StringOccurrenceDemo()
{
  var aString = "Dura lex, sed lex";
  var aSubString = "lex";
  var Res = aqString["FindLast"](aString,aSubString);
  if (Res != -1)
    Log["Message"]("The last occurrence of the '" + aSubString + "' substring within the '" + aString + "' string is at position " + Res)
  else
           Log["Message"]("There are no occurrences of the '" + aSubString + "' substring in '" + aString + "'")
}

// Result:
// The last occurrence of the 'lex' substring within the 'Dura lex, sed lex' string is at position 14

See Also

Working With Strings
Find Method
SubString Method
Replace Method
Remove Method

Highlight search results