Sunday, November 15, 2009

TestComplete: Working with Strings

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

Basics

Special characters

Getting the string length

Concatenating strings

Comparing strings

Accessing individual character of a string

Searching for characters and substrings

Getting a substring

Splitting strings

Removing extra spaces from a string

Replacing characters and substrings

Changing the letter case

Working with string lists

Basics

A String is a sequence of symbols or digits. Strings are among the most frequently used data types. Like any other data type, strings in TestComplete are represented as OLE-compatible variants. In VBScript, a sequence of literal characters, enclosed in double quotes ("), is recognized as a string. Single quotation marks (') are allowed within a string. To insert a double quotation mark into a string, it should be duplicated. The following is an example of string:

VBScript

str1 = "The brig was heading to Liverpool, when the captain noticed a ship."
str2 = "'Ahoy! Is there anyone?' - the captain cried."
str3 = """Nobody."" - was the answer."

To work with strings, TestComplete has a special scripting object - aqString. The object is available for all supported scripting languages, so you can use it to work 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 can be used to work with strings is aqConvert. This object has several methods that convert values of different types to a string 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.StrToTime

Converts the specified string to a time value.

aqConvert.VarToStr

Converts the specified variant value to a string.

Furthermore, you can use native VBScript functions that operate with strings. A detailed description for these functions can be found at MSDN (the online version is available at http://msdn.microsoft.com). The table below only lists major functions:

Function

Description

Asc(string)

Returns the ASCII character code corresponding to the first letter in a string.

Chr(charcode)

Returns the character associated with the specified ANSI character code.

CStr(expression)

Returns an expression that has been converted to a variant of sub-type string.

Escape(charString)

Encodes a string so it only contains ASCII characters. Non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character.

InStr([startpos, ]string1, string2[, compare])

Returns the position of the first occurrence for one string within another.The search can start at a given position and use binary (compare=0) or text (compare=1) comparisons.

InStrRev(string1, string2[, start[, compare]])

Returns the position of an occurrence for one string within another, from the end of string. The search can start at a given position and use binary (compare=0) or text (compare=1) comparisons.

Join(list[, delimiter])

Returns a string created by joining a number of substrings contained in an array.

LCase(string)

Returns a string that has been converted to lowercase.

Left(string, length)

Returns a specified number of characters from the left side of a string.

Len(string | varname)

Returns the number of characters in a string or the number of bytes required to store a variable.

LTrim(string)

Returns a copy of a string without leading spaces.

Mid(string, start[, length])

Returns a specified number of characters from a string.

Replace(string, findstr, replacewith[, start[, count[, compare]]])

Returns a string in which a specified substring has been replaced with another substring a specified number of times.

Right(string, length)

Returns a specified number of characters from the right side of a string.

RTrim(string)

Returns a copy of a string without trailing spaces.

Space(number)

Returns a string consisting of the specified number of spaces.

Split(expression[, delimiter[, count[, compare]]])

Returns a zero-based, one-dimensional array containing a specified number of substrings.

StrComp(string1, string2[, compare])

Returns a value indicating the result of a string comparison.

String(number, character)

Returns a repeating character string of the length specified.

StrReverse(string)

Returns a string in which the character order of a specified string is reversed.

Trim(string)

Returns a copy of a string without leading and trailing spaces.

UCase(string)

Returns a string that has been converted to uppercase.

Unescape(charString)

Decodes a string encoded with the Escape function.

Special characters

In VBScript you can emulate any character by using the Chr function with the appropriate ASCII code. This also applies to special characters that are used to format string values. Alternatively, you can emulate control characters using native string constants. The table below lists the most frequently used special characters and their constants.

Description

Character sequence

Carriage return.

Chr(13) -- or -- vbCr

Line feed.
On Unix platforms it is interpreted as new line.

Chr(10) -- or -- vbLf

A combination of carriage return and line feed.
On Windows platforms it is interpreted as new line.

Chr(13)+Chr(10) -- or -- VbCrLf

New line.
Either a line feed character or a combination of carriage return and line feed.

vbNewLine

Form feed.

Chr(12) -- or -- vbFormFeed

Horizontal tab.

Chr(9) -- or -- vbTab

Vertical tab.

Chr(11) -- or -- vbVerticalTab

1 comment:

  1. Dear Nitin Sharma,
    thank you for providing some solutions on how to use TestComplete to your readers. Could you please add a source for your post as the materials you posted are taken from AutomatedQA TestComplete's Online Help file, available here:
    http://www.automatedqa.com/support/viewarticle.aspx?aid=3686

    ReplyDelete

I welcome your comment, will respond and post it at the earliest:)