aqString.Format Method

Applies to TestComplete 15.70, last modified on December 17, 2024

Description

The Format method replaces special symbols known as format specifiers with formatted values of subsequent arguments. The first format specifier controls the output of the first argument, the second specifier - the output of the second argument, and so on. The total number of arguments can vary, but it should be equal to the number of format specifiers. If there are more arguments than format specifications, the extra arguments are ignored.

Declaration

aqString.Format(Format, Argument1, Argument2, ..., ArgumentN)

Format [in]    Required    String    
Argument1 [in]    Optional    Variant    
Argument2 [in]    Optional    Variant    
...
ArgumentN [in]    Optional    Variant    
Result String

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Format

Specifies the string to be processed. The string can contain ordinary characters, special characters and format specifiers.

Argument1, Argument2, ..., ArgumentN

Specify the values to be converted to some format and to be substituted with corresponding format specifiers.

Result Value

The string with the actual values of passed arguments which are formatted as required.

Remarks

Python and JavaScript have built-in functionality similar to the aqString.Format method.

In Python, you can use the str.format() method or %-interpolation.

Python

import math

def Test():
  n = 5
  sq_root = math.sqrt(n)

  # Using str.format()
  Log.Message("The square root of {n} is {sq_root:.2f} (roughly).".format(n=n, sq_root=sq_root))
  # Using %-interpolation
  Log.Message("The square root of %d is %.2f (roughly)." % (n, sq_root))

# Result:
# The square root of 5 is 2.24 (roughly).

# Same as:
# Log.Message(aqString.Format("The square root of %f is %f", n, sq_root))

In JavaScript (not JScript), you can use template literals– strings enclosed in `back-ticks` instead of quotes – to embed expressions in strings without concatenation. However, unlike aqString.Format, template strings do not support value formatting.

JavaScript

var n = 25;
Log.Message(`The square root of ${n} is ${Math.sqrt(n)}`);
// The square root of 25 is 5

// Same as:
// Log.Message(aqString.Format("The square root of %f is %f", n, Math.sqrt(n)));
// Log.Message("The square root of " + n + " is " + Math.sqrt(n));

Example

The following example demonstrates the aqString.Format method usage with various format specifiers.

JavaScript, JScript

function FormatDemo()
{
     Log.Message(aqString.Format("This is a string: %s", "Some String"))
     // This is a string: Some String

     Log.Message(aqString.Format("The hexadecimal notation for %i is %X.", 255, 255))
     // The hexadecimal notation for 255 is FF.        

     Log.Message(aqString.Format("This is a floating-point number in exponential notation: %.2e", 123456789.123456789))    
     // This is a floating-point number in exponential notation: 1.23e+008
}

Python

def FormatDemo():
  Log.Message(aqString.Format("This is a string: %s", "Some String"))
  # This is a string: Some String
  Log.Message(aqString.Format("The hexadecimal notation for %i is %X.", 255, 255)) 
  # The hexadecimal notation for 255 is FF.
  Log.Message(aqString.Format("This is a floating-point number in exponential notation: %.2e", 123456789.123456789))    
  # This is a floating-point number in exponential notation: 1.23e+008

VBScript

Sub FormatDemo    
     Log.Message(aqString.Format("This is a string: %s", "Some String"))
     ' This is a string: Some String

     Log.Message(aqString.Format("The hexadecimal notation for %i is %X.", 255, 255))
     ' The hexadecimal notation for 255 is FF.        

     Log.Message(aqString.Format("This is a floating-point number in exponential notation: %.2e", 123456789.123456789))    
     ' This is a floating-point number in exponential notation: 1.23e+008
End Sub

DelphiScript

procedure FormatDemo;
begin
     Log.Message(aqString.Format('This is a string: %s', 'Some String'));
     // This is a string: Some String

     Log.Message(aqString.Format('The hexadecimal notation for %i is %X.', 255, 255));
     // The hexadecimal notation for 255 is FF.        

     Log.Message(aqString.Format('This is a floating-point number in exponential notation: %.2e', 123456789.123456789));    
     // This is a floating-point number in exponential notation: 1.23e+008
end;

C++Script, C#Script

function FormatDemo()
{
     Log["Message"](aqString["Format"]("This is a string: %s", "Some String"))
     // This is a string: Some String

     Log["Message"](aqString["Format"]("The hexadecimal notation for %i is %X.", 255, 255))
     // The hexadecimal notation for 255 is FF.        

     Log["Message"](aqString["Format"]("This is a floating-point number in exponential notation: %.2e", 123456789.123456789))    
     // This is a floating-point number in exponential notation: 1.23e+008
}

See Also

Working With Strings
Format Specifiers

Highlight search results