JScript - Working With Strings

Applies to TestComplete 15.64, last modified on June 12, 2024

This topic contains information about handling strings in JScript and provides examples of operations that deal with strings. It contains the following sections:


A String is a sequence of symbols or digits. Strings are among the most frequently used data types. Like any other data types, strings in TestComplete are represented as OLE-compatible variants.

In JScript, a sequence of literal characters enclosed in a matching pair of quotation marks is recognized as a string. The quotation marks could be either single (') or double ("). The quotation characters are allowed within a string if they do not match the enclosing pair. All the following strings are valid:


str1 = "The brig was heading to Liverpool, when the captain noticed a ship.";
str2 = 'It came out of night fog and looked weird: no sails, no lights and no crew.';
str3 = "'Ahoy! Is there anyone?' - the captain cried.";
str4 = '"Nobody." - was the answer.'

To deal with strings, TestComplete has a special aqString scripting object. The object is available for all supported scripting languages, so that you can use it to operate with string values regardless of the chosen language.

Method, Property Description
aqString.AddListItem Adds a new item to a string list.
aqString.ChangeListItem Changes the value of the string list item with the given index.
aqString.Compare Compares two specified strings.
aqString.Concat Concatenates two specified strings.
aqString.DeleteListItem Removes an item with the given index from a string list.
aqString.Find Searches for a substring within the given string. Use this method instead of the obsolete aqString.Contains.
aqString.Format Generates a formatted string.
aqString.GetChar Retrieves a single character from the input string.
aqString.GetLength Returns the number of characters in a string.
aqString.GetListItem Returns an individual item from the list passed through the input string.
aqString.GetListLength Returns the number of items in the string list.
aqString.Insert Inserts one string to another at the specified position.
aqString.ListSeparator Specifies a character used to separate individual values in a list.
aqString.Quote Encloses the specified string in quotes.
aqString.QuoteSymbol Specifies a symbol used as a quotation mark.
aqString.Remove Removes a number of characters from the input string.
aqString.Replace Replaces all the occurrences of one substring with another substring.
aqString.SubString Retrieves a substring from the input string.
aqString.ToLower Converts the specified string to lower case.
aqString.ToUpper Converts the specified string to upper case.
aqString.Trim Removes spaces and control characters from the specified string.
aqString.Unquote Converts a quoted string to an unquoted string.

Another scripting object that is useful for string manipulation is aqConvert. This object has several methods that convert values of different types to a string representation and vice versa.

Method Description
aqConvert.CurrencyToFormatStr Converts a currency value to a string using the specified format settings.
aqConvert.CurrencyToStr Converts a currency value to a string.
aqConvert.DateTimeToFormatStr Converts the given date value to a string using the specified format.
aqConvert.DateTimeToStr Converts the given date value to a string.
aqConvert.FloatToStr Converts a floating-point value to a string.
aqConvert.IntToStr Converts the given number into a string.
aqConvert.StrToCurrency Converts the specified string to a currency value.
aqConvert.StrToDate Converts the specified string to a date value.
aqConvert.StrToDateTime Converts the specified string to a date/time value.
aqConvert.StrToFloat Converts the specified string to a floating-point value.
aqConvert.StrToInt Converts the specified string to an integer value.
aqConvert.StrToInt64 Converts the specified string to a long integer value.
aqConvert.StrToTime Converts the specified string to a time value.
aqConvert.VarToStr Converts the specified variant value to a string.

Furthermore, JScript has its own inherent object String, that is, a wrapper for a string data type. The strings that are used in TestComplete are compatible with this object, moreover, the String objects are created implicitly for every string. That is, you can call the object methods not only for object instances but for any TestComplete string. For example, the following method call is correct:


var myStr, mySubStr;
myStr = "This is my string";
mySubStr = myStr.substr(11,6);

A detailed description for the String object is provided in the MSDN library, the table below lists its major properties and methods:

Property, Method Description
length Property. Returns the length of the string.
charAt(index) Method. Returns the character at the specified position.
charCodeAt(index) Method. Returns an integer representing the Unicode encoding of the character at the specified location.
concat([string2[, string3[, . . . [, stringN]]]]) Method. Returns a string value containing the concatenation of two or more supplied strings.
fromCharCode([code1[, code2[, ...[, codeN]]]]) Method. Returns a string that corresponds to the specified Unicode character numbers.
indexOf(subString[, startIndex]) Method. Returns the character position where the first occurrence of a substring occurs.
localeCompare(stringExp) Method. Returns a value indicating whether two strings are equivalent in the current locale.
match(rgExp) Method. Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
replace(rgExp, replaceText) Method. Returns a copy of a string with text replaced using a regular expression or search string.
search(rgExp) Method. Returns the position of the first substring match in a regular expression search.
slice(start, [end]) Method. Returns a section of a string.
split([separator[, limit]]) Method. Returns the array of strings that results when a string is separated into substrings.
substr(start [, length ]) Method. Returns a substring beginning at a specified location and having a specified length.
substring(start, end) Method. Returns the substring at the specified location within a string.
toLocaleLowerCase() Method. Returns a string where all alphabetic characters have been converted to lowercase, taking into account the current locale.
toLocaleUpperCase() Method. Returns a string where all alphabetic characters have been converted to uppercase, taking into account the current locale.
toLowerCase() Method. Returns a string where all alphabetic characters have been converted to lowercase.
toUpperCase() Method. Returns a string where all alphabetic characters have been converted to uppercase.
toString([radix]) Method. Returns a string representation of an object.

Special characters

There are several pairs of symbols that are used to represent special characters in JScript strings. They all start with a backslash character (\), and are often called escape characters. The following escape characters are allowed in JScript.

Character sequence Description
\\ Backslash
\b Backspace
\r Carriage return
\" Double quote
\f Form feed
\n New line
\' Single quote
\t Tab

Getting the string length

To obtain the total number of characters in a string you can either read the length property of the JScript String object, or call the aqString.GetLength method. The character position in JScript is zero-based, so the maximum position number in a string is Length-1. The following code demonstrates both ways of obtaining the string length:


function StringLengthDemo()
  var aString = "Some text";
  Log.Message("The string is " + aString.length + " character(s) long.");
  Log.Message("The string is " + aqString.GetLength(aString) + " character(s) long.");

Using TestComplete, you can limit the length of string parameters returned by functions of the tested application. For this purpose, use the Maximum string length of [out] parameters project property. If the length of the string returned from the application via one of its out parameters exceeds the property’s value, TestComplete treats the returned string as a null one.

Concatenating strings

The operation that forms a string out of several others is called concatenation. The native String object of JScript, and the aqString object have special methods (respectively, String.concat and aqString.Concat) that perform this operation.

However, it is much more convenient to use the addition operator (+) for this purpose. If one of the addition operands is a string, then the performed operation is considered to be string concatenation rather than number addition. In this case all other operands are converted to a string and the resulting string is formed.

This sample code shows how to use all three techniques:


function ConcatenationDemo()
  var Str1 = "String No 1 ";
  var Str2 = "String No 2 ";
  var Str3 = "String No 3 ";

  Log.Message(Str1.concat(Str2, Str3));
  Log.Message(aqString.Concat(Str1, Str2));
  Log.Message(Str1 + "String No 2 " + "String No " + 3 + " ");

Comparing strings

The comparison of two strings is widely used during testing. Generally a test procedure obtains textual data (user input, file contents, property value and so on) and then compares it with the expected data. The TestComplete aqString and native JScript String objects have their methods for comparing one string value to another. These methods are: aqString.Compare and String.localeCompare.

The aqString.Compare method has three parameters: two of them - String 1 and String2 specify the strings to be compared, while the last parameter defines whether the comparison should be case-sensitive or not.

JScript’s native Str1.localeCompare(Str2) method compares the text of a current object instance (Str1) against another string specified by Str2 parameter. The comparison is case-sensitive and is made according to local settings. The method returns -1 if Str1 is less than Str2, 1 if Str1 is greater than Str2 and 0 if the strings are the same.

Both of the methods can compare strings holding specific national symbols, for example characters with diacritical marks ( , , and others).

The code below demonstrates how to use both of mentioned methods:


// This helper function obtains an integer comparison result from the
// localeCompare routine and returns a textual interpretation of the result.
function GetComparisonStr(IntegerComparisonResult)
  if (IntegerComparisonResult == 0)
        return IntegerComparisonResult + '. The strings are the same.';
        return IntegerComparisonResult + '. The strings are different.';

function StringComparison()
  // Using the method of the native String object
  Log.Message("\"Abra\".localeCompare(\"abra\"): " + GetComparisonStr("Abra".localeCompare("abra")));
  Log.Message("\"abra\".localeCompare(\"Abra\"): " + GetComparisonStr("abra".localeCompare("Abra")));
  Log.Message("\"abra\".localeCompare(\"abra\"): " + GetComparisonStr("abra".localeCompare("abra")));

  // Using the method of the aqString object
  Log.Message("aqString.Compare(\"Abra\", \"abra\", false): " + aqString.Compare("Abra","abra", false));
  Log.Message("aqString.Compare(\"Abra\", \"abra\", true): " + aqString.Compare("Abra","abra", true));

  // Dealing with native-language characters
  // The following code assumes that French layout is installed on computer
  // 040C - is the identifier of the French layout
  if ( aqEnvironment.IsLanguageSupported(0x040C) && aqEnvironment.SetKeyboardLayout(Sys.Process("TestComplete").Id, "0000040c"))
    Log.Message("\"français\".localeCompare(\"Français\"): " + GetComparisonStr("français".localeCompare("Français")));
    Log.Message("\"français\".localeCompare(\"français\"): " + GetComparisonStr("français".localeCompare("français")));
    Log.Message("\"français\".localeCompare(\"francais\"): " + GetComparisonStr("français".localeCompare("francais")));
    Log.Message("aqString.Compare(\"français\", \"Français\", false): "+aqString.Compare("français", "Français", false));
    Log.Message("aqString.Compare(\"français\", \"Francais\", false): "+aqString.Compare("français", "Francais", false));
    Log.Message("aqString.Compare(\"français\", \"Français\", true): "+aqString.Compare("français", "Français", true));

Accessing individual character of a string

The JScript scripting language does not have a data type that is intended for storing single symbols. However, this is not a problem, since the string type can hold a series of characters as well as individual characters. Moreover, you can extract a single character out of the string using the aqString.GetChar and String.charAt methods. The sample routine below illustrates their usage, it posts the given text to the TestComplete log in two different ways: as a whole string and by a single letter.


function StringByLetter()
  var aString = "Per aspera ad astra";
  Log.Message("The string is : " + aString);

  Log.Message("And now this text letter by letter using aqString.GetChar:")
  for (i = 0;i < aqString.GetLength(aString); i++)
    Log.Message(aqString.GetChar(aString, i));

  Log.Message("And now this text letter by letter using String.charAt:")
  for (i = 0;i < aString.length; i++)

Searching for characters and substrings

One of the most common tasks that one has to perform when working with string values is determining whether specific text is part of a string. To perform such tasks, the aqString object has the Find method. If the specified substring was found, the method returns the number of the first occurrence of the substring within the source string. If the specified substring was not found, the method returns -1:


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

You can also get the position where the specified substring occurs using the native method indexOf of the JScript String object. If the substring was found, the method returns the initial position (from 0) of the first substring match. If no occurrences were found, the method returns -1. The code below demonstrates how to use it:


function TextPosDemo()
  var aString = "Per aspera ad astra";
  var aSubString = "astra";

  var findpos = aString.indexOf(aSubString);
  if (findpos != -1)
    Log.Message("A substring '" + aSubString + "' was found at position " + findpos);
    Log.Message("There are no occurrences of '" + aSubString + "' in '" + aString + "'.");

Getting a substring

The aqString object and the native String object of JScript have several methods that extract a substring out of a string. They are SubString, substring, substr and slice. These methods vary in how the extracted substring is defined.

The String.substring method returns a text fragment that lies between starting and ending character positions. The character at the ending position is not included. The position is zero-based. If starting position is greater than ending position, then these values are swapped. The negative values are considered as zeros.

The String.slice method is similar to substring, yet, it has its own specifics. In this method the substring is also specified by the starting and ending positions. However the ending position can be omitted, in this case the substring lasts until the end of the initial string. If the starting position is greater than the ending position, then the empty string is returned. The negative values are allowed and are interpreted as position calculated as a given absolute value subtracted from the string length.

The SubString returns a text fragment of the input string that starts at the given position and has the specified length. The String.substr method works in a similar way, however it does not require the input string to be passed as the parameter since every String object instance already refers to a string. Thus the String.substr method has only two parameters, the first one specifies a starting position, while the second defines the length of the retrieved fragment. The length parameter is optional, and if it is omitted then the resulting substring continues up to the end of the source string. If length parameter is negative or zero then the empty string is returned. Undefined, negative and zero values of the position parameter are interpreted as the first character in a string.

The sample code below demonstrates how to use all these methods:


function GetStringDemo()
  var Str = "0123456789";

  Log.Message("The 'String.substring' method demo:")
  Log.Message(Str.substring(2, 7)); // Posts "23456"
  Log.Message(Str.substring(7, 2)); // Posts "23456"
  Log.Message(Str.substring(-2, 7)); // Posts "0123456"
  Log.Message(Str.substring(2, 2)); // Posts ""

  Log.Message("The 'String.slice' method demo:")
  Log.Message(Str.slice(2, 7)); // Posts "23456"
  Log.Message(Str.slice(2)); // Posts "23456789"
  Log.Message(Str.slice(-8, -3)); // Posts "23456"
  Log.Message(Str.slice(7, 2)); // Posts ""

  Log.Message("The 'aqString.SubString' method demo:")
  Log.Message(aqString.SubString(Str, 2, 5)); // Posts "23456"
  Log.Message(aqString.SubString(Str, 2, 0)); // Posts ""

  Log.Message("The 'String.substr' method demo:")
  Log.Message(Str.substr(2, 5)); // Posts "23456"
  Log.Message(Str.substr(2)); // Posts "23456789"
  Log.Message(Str.substr(-2, 3)); // Posts "012"
  Log.Message(Str.substr(2, 0)); // Posts ""

Splitting strings

Sometimes it is required to make several strings out of a single string. This operation splits a string into substrings. It can be performed by the native split method of JScript. This method searches the string for delimiter character(s) (defined either as string or as a regular expression), separates the string and returns an array holding the constituent strings. Also you can constrain the maximal array length with the third parameter. It is not obligatory and can be omitted. The same method can be used to split a string onto substrings, sentences and even separate words, it all depends on the specified delimiter. The first sample routine below uses a space character as a delimiter to extract words out of a string and the second routine splits the string by line breaks:


function SplitDemo1()
    var s = "Better late than never but better never late.";
    // Split at each space character.
    var ss = s.split(" ");
    Log.Message("The resulting array is: " + ss);
    Log.Message("There are " + ss.length + " words in the array");
    Log.Message("The first word is: " + ss[0]);

function SplitDemo2()
    var s = "Better late than never\r\nbut better never late.";
    // Split at line break character.
    var ss = s.split("\r\n");
    Log.Message("The resulting array is: " + ss);
    Log.Message("There are " + ss.length + " words in the array");
    Log.Message("The first word is: " + ss[0]);

TestComplete has a similar method called aqString.GetListItem. It extracts a substring with the specified index out of the input string. It was designed to read the items from string lists, see Working with string lists for more information. However, it also redefines the delimiter characters and, like the split method, can be used to get sentences, separate words and so forth.


function SplitDemo3()
    var s = "Better late than never but better never late.";
    // Assign list separator to space character
    var prevSep = aqString.ListSeparator;
    aqString.ListSeparator = " ";
    // Split by spaces
    Log.Message("There are " + aqString.GetListLength(s) + " words in a string");
    Log.Message("The first word is: " + aqString.GetListItem(s,0));
    // Restore previous separator
    aqString.ListSeparator = prevSep;

Removing extra spaces from a string

The aqString object has a special routine aqString.Trim that excludes the leading, trailing spaces, or both, from a string. Generally this method is applied to remove the "useless" spaces in the beginning or end of the strings obtained from the user input.


function TrimDemo()
  var str="    Hallo    ";
  Log.Message("'" + aqString.Trim(str, aqString.stLeading) + "'"); // Posts 'Hallo    '
  Log.Message("'" + aqString.Trim(str, aqString.stTrailing) + "'"); // Posts '    Hallo'
  Log.Message("'" + aqString.Trim(str, aqString.stAll) + "'"); // Posts 'Hallo'

Another function that can be useful when handling user input strings is excluding extra inner spaces out of the string. This function seems to be similar to Trim, but the latter only removes spaces at the beginning or end of the string and does not affect the spaces inside the string. The general idea of the function is for the string to be parsed into separate words and then a new string is constructed. The new string consists of the same words but is separated with a single space between words.


function TrimInner(Str)
    var WordArray = Str.split(/ +/);
    Str = "";
    for (i = 0; i < WordArray.length; i++) Str += (WordArray[i] + " ");
    return aqString.Trim(Str);

//An example of how to use this function
function TrimInnerDemo()
  Log.Message(TrimInner("Follow the      white rabbit"));

Replacing characters and substrings

Quite often we need to find and replace a character or substring within a string. There are two ways to do this: by using the aqString.Replace method or using the replace method of a String object.

The aqString object method is much easier to use and can be applied when you need to change a definite character or string. It allows you to set whether the search should be case-sensitive or not. Here is an example of how to use this method:


function StringReplaceDemo()
  var str = "Hi, Bob. Have you seen Bob Robbinson?";
  str = aqString.Replace(str, "Bob", "Jack", true);

The String.replace method is a little more complicated, but it offers more flexibility. You can change not only a definite character or string, but all fragments matching the specified regular expression pattern. The regular expression pattern is defined between two backslash ("/") characters, additionally you can set the following flags that can be combined: g - perform global search for all occurrences of pattern, i - ignore letter case and m - perform a multiline search. For a full description of how to use regular expressions refer to the Introduction to Regular Expressions article in the MSDN library.

The first sample demonstrates how to change a definite string using the replace method.


function RegExReplaceDemo1()
  var str = "Hi, Bob. Have you seen Bob Robbinson?";
  // Define regular expression pattern
  var re = /Bob/g;
  // Perform replace operation
  str = str.replace(re, "Jack");

The second example shows how to replace a substring with alternative parts. The patterns of alternative parts are separated by pipe characters (" | "). For instance in the sample below the /ht(ml|m)/ pattern matches both html and htm:


function RegExReplaceDemo2()
  var str = "The html is widely used in Internet. The HTM file is a text file with tags.";
  // Define regular expression pattern.
  var re = /ht(ml|m)/gi;
  // Perform replace operation
  str = str.replace(re, "hypertext markup language");

Furthermore, using regular expressions you can search for the text fragments matching the specified format. In the next sample, all dates written in the DD/MM/YYYY format are substituted with the Some Date string. This operation can be useful, for example, when comparing two reports that contain the generation date.


function RegExReplaceDemo3()
  var str = "Date of report: 30/04/2005.";
  // Define regular expression pattern.
  var re = /\d{1,2}.\d{1,2}.\d{2,4}/g;
  // Perform replace operation
  str = str.replace(re, "Some Date");

Changing the letter case

Capital and lower case letters can be included in a string. The TestComplete aqString object and native String object have methods that convert uppercased letters to lower case and vice versa. They are: aqString.ToLower, aqString.ToUpper, String.toLowerCase and String.toUpperCase.

The code below demonstrates how all of these methods are applied.


function LetterCaseDemo()
  var str="The word 'Champagne' is of French origin"
  // Converting to lower case
  // Converting to upper case

Working with string lists

Some scripting objects, generally, controls like ListBoxes, ComboBoxes, Memos, return data about their state or contents as string lists. The individual data elements (or items) in this list are separated by commas, line breaks, carriage returns or some other delimiter characters.

The aqString object has a number of specific methods (AddListItem, ChangeListItem, DeleteListItem, GetListItem and GetListLength) that are intended to deal with such lists of textual data. The AddListItem and DeleteListItem append the item to the list and remove the item from the list. The GetListItem method retrieves the item with the given index, and ChangeListItem assigns a new value to the given item. The GetListLength method returns the total number of items in the string list.

The symbol that is used as the separator of list items is defined by the ListSeparator property. By default the list separator is a pipe character "|", but it can be reassigned to comma, column, line break, carriage return, tabulation, and any other printable and non-printable character, or even to several characters.

Here is the sample code that shows how to operate with the string lists returned by scripting objects.


function ListDialogOptions()
    // Get a string with dialog options
    var OptStr = UserForms.UserForm1.SaveDialog1.Options;
    // Assign list separator to comma
    var prevSep = aqString.ListSeparator;
    aqString.ListSeparator = ",";
    // Get the number of dialog options
    Log.Message("The dialog has " + aqString.GetListLength(OptStr) + " option(s) enabled:");
    // Iterate through the options list
    for (i = 0; i < aqString.GetListLength(OptStr); i++)
      //Get the option and post it to log
      Log.Message("Option No " + (i+1) + " is: "+ aqString.GetListItem(OptStr, i));
    // Restore previous separator
    aqString.ListSeparator = prevSep;

function ManageMemoText()
    // Get a string with memo lines
    var StrList = UserForms.UserForm1.cxMemo1.Lines.Text;
    // Post the memo contents to log
    // Assign list separator to newline character
    var prevSep = aqString.ListSeparator;
    aqString.ListSeparator = "\n";
    // Append one more line
    StrList = aqString.AddListItem(StrList, "Last Line");
    // Change the value of the first line
    StrList = aqString.ChangeListItem(StrList, "New First Line",0);
    // Set the memo contents to a new list
    UserForms.UserForm1.cxMemo1.Lines.Text = StrList;
    // Post the memo contents to log
    // Restore previous separator
    aqString.ListSeparator = prevSep;

See Also

Working With Strings
aqString Object
aqConvert Object

Highlight search results