aqString.Compare Method

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

Description

Use the aqString.Compare method to compare two strings. The comparison can be case-sensitive or case-insensitive.

Declaration

aqString.Compare(String1String2CaseSensitive)

String1 [in]    Required    String    
String2 [in]    Required    String    
CaseSensitive [in]    Required    Boolean    
Result Integer

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

String1 and String2

The strings to be compared.

CaseSensitive

Indicates whether the comparison should be case-sensitive (True) or case-insensitive (False).

Result Value

Integer value that indicates the comparison result. The following values are possible:

Value Meaning
-1 The String1 is less than String2.
0 The String1 identical to String2.
1 The String1 is greater than String2.

Remarks

String comparison uses character codes and is not affected by the locale. If the CaseSensitive parameter is True, letter case is taken into account ("a" is greater than "A"), otherwise it is ignored ("a" is equal to "A"). The comparison is performed symbol-by-symbol and finishes once a difference is found or when both strings have been compared to the end. If two strings having different lengths compare as equal to the end of one string, the longer string is considered as the greater one (for example, "abcd" is greater than "ab").

Example

The following example demonstrates how you can compare two strings:

JavaScript, JScript

function StringComparisonSample()
{
  var str1 = "chart";
  var str2 = "graph";
  var strFormat;

  switch (aqString.Compare(str1, str2, false))
  {
    case -1 : strFormat = "\"%s\" is less than \"%s\".";    break;
    case 0  : strFormat = "\"%s\" and \"%s\" are equal.";   break;
    case 1  : strFormat = "\"%s\" is greater than \"%s\"."; break;
  }

  Log.Message(aqString.Format(strFormat, str1, str2));
}

Python

def StringComparisonSample():
  str1 = "chart"
  str2 = "graph"
  if aqString.Compare(str1, str2, False) == -1:
    strFormat = "\"%s\" is less than \"%s\"."
  elif aqString.Compare(str1, str2, False) == 0:
    strFormat = "\"%s\" and \"%s\" are equal."
  elif aqString.Compare(str1, str2, False) == 1:
    strFormat = "\"%s\" is greater than \"%s\"."
  Log.Message(aqString.Format(strFormat, str1, str2))

VBScript

Sub StringComparisonSample
  Dim str1, str2, strFormat
  str1 = "chart"
  str2 = "graph"

  Select Case aqString.Compare(str1, str2, False)
    Case -1  strFormat = """%s"" is less than ""%s""."
    Case 0   strFormat = """%s"" and ""%s"" are the same."
    Case 1   strFormat = """%s"" is greater than ""%s""."
  End Select
  
  Log.Message aqString.Format(strFormat, str1, str2)
End Sub

DelphiScript

procedure StringComparisonSample;
var str1, str2, strFormat;
begin
  str1 := 'chart';
  str2 := 'graph';

  case aqString.Compare(str1, str2, false) of
    -1 : strFormat := '"%s" is less than "%s".';
     0 : strFormat := '"%s" and "%s" are equal.';
     1 : strFormat := '"%s" is greater than "%s".';
  end;

  Log.Message(aqString.Format(strFormat, str1, str2));
end;

C++Script, C#Script

function StringComparisonSample()
{
  var str1 = "chart";
  var str2 = "graph";
  var strFormat;

  switch (aqString["Compare"](str1, str2, false))
  {
    case -1 : strFormat = "\"%s\" is less than \"%s\".";    break;
    case 0  : strFormat = "\"%s\" and \"%s\" are equal.";   break;
    case 1  : strFormat = "\"%s\" is greater than \"%s\"."; break;
  }

  Log["Message"](aqString["Format"](strFormat, str1, str2));
}

See Also

Working With Strings
Find Method
StrMatches Method

Highlight search results