Working with Strings - Part 1: Basics
Test and Monitor | Posted February 19, 2007

A String is a sequence of charactors and/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.

Depending on the scripting Language, strings are defined in different ways:

  • In VBScript, a string is considered as a sequence of literal characters, enclosed in double quotes ("). Single quotation marks (') are allowed within a string.
  • In JScript, C++Script and C#Script, a string is considered a sequence of literal characters enclosed in a matching pair of quotation marks. 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.
  • In DelphiScript, a string is considered a sequence of literal characters enclosed in single quotation marks (') is recognized as a string. Double quotation marks (") are allowed within a string.

The Utilities object contains a number of methods to help work with strings. The object is available for all supported scripting languages, so that you can use it to operate with date values regardless of the chosen language. The object’s name is optional and can be omitted.

Method Description
AdjustLineBreaks Replaces all single carriage return (CR) or linefeed (LF) symbols in the specified string with the carriage return and linefeed pair (CR/LF).
AnsiCompareStr Performs a case-sensitive comparison of two strings. The result is an integer value indicating whether the first string is longer than the second, the strings are identical, or the second is longer than the first string. The comparison is made according to local settings.
AnsiCompareText Performs a case-insensitive comparison of two strings. The result is an integer value indicating whether the the first string is longer than the second, the strings are identical, or the second is longer than the first string. The comparison is made according to local settings.
AnsiExtractQuotedStr Converts a quoted string into an unquoted string.
AnsiLastChar Returns the last character in the string.
AnsiLowerCase Converts all letters of the given string to lowercase.
AnsiQuotedStr Encloses the specified string in the specified characters.
AnsiSameStr Performs a case-sensitive comparison of two strings. The result is a Boolean value indicating whether the strings are identical. The comparison is made according to local settings.
AnsiSameText Performs a case-insensitive comparison of two strings. The result is a Boolean value indicating whether the strings are identical. The comparison is made according to local settings.
AnsiStrScan Searches the specified string from left to right for the specified character.
AnsiStrRScan Searches the specified string from right to left for the specified character.
AnsiUpperCase Converts all letters of the given string to uppercase.
CompareStr Performs a case-sensitive comparison of two strings. The result is an integer value indicating whether the the first string is longer than the second, the strings are identical, or the second is longer than the first string. The comparison is not affected by local settings.
CompareText Performs a case-insensitive comparison of two strings. The result is an integer value that indicates whether the first string is longer than the second, the strings are identical, or the second is longer than the first string. The comparison is not affected by local settings.
IsDelimiter Indicates whether the specified character in the given string is one of the delimiters.
LastDelimiter Locates the last delimiter in the specified string.
LowerCase Converts all letters of the given string to lowercase. Affects only ASCII characters.
QuotedStr Encloses the specified string in quotes.
SameText Performs a case-insensitive comparison of two strings. The result is a Boolean value indicating whether the strings are identical. The comparison is not affected by local settings.
StringReplace Replaces occurrences of the specified substring in the given string.
StrLCat Adds the given string to the end of another string and cuts the resulting string, if necessary.
StrLComp Compares up to a specified number of characters in the given strings. The comparison is case-sensitive.
StrLIComp Compares up to a specified number of characters in the given strings. The comparison is case-insensitive.
Trim Removes the leading and trailing spaces as well as control characters from the specified string.
TrimLeft Removes the leading spaces and control characters from the specified string.
TrimRight Removes the trailing spaces and control characters from the specified string.
UpperCase Converts all letters of the given string to uppercase. Affects only ASCII characters.
WrapText Splits the specified string into multiple lines if its length reaches the given value or if there are occurrences of any of the specified characters.

Also this object has several methods that convert values of different types to a string representation and vice versa.

Method Description
CurrToStr Converts the specified currency value to its string representation.
CurrToStrF Converts the given currency value to a string according to the specified precision and format.
DateTimeToStr Converts the variant value that stores the date and time to a string.
DateTimeToString Converts a variant value storing the date and time to a string using the format specified.
DateToStr Converts a variant value holding a date to a string.
FloatToStr Converts a floating-point value to a string.
FloatToStrF Converts a floating-point value to a string according to the specified format settings.
Format Converts the items of the given array to a string using the specified format.
FormatCurr Converts the given currency value to a string using the specified format.
FormatDateTime Converts the given date and time value to a string using the specified format.
FormatFloat Converts a floating-point value to a string using the specified format.
IntToStr Converts the given number into a string.
NegCurrFormat Specifies the format string used to convert negative currency values to strings.
StrToCurr Converts the string holding a floating-point value to the corresponding currency value.
StrToDate Converts the string of a special format to a value that encodes date.
StrToDateTime Converts the string of a special format to a value that encodes date and time.
StrToFloat Converts the specified string to a floating-point value.
StrToInt Converts the specified string to an integer value.
StrToIntDef Converts the specified string to an integer value. If the string is invalid then the specified default value is returned.
StrToTime Converts the string of a special format to a value that encodes time.
TimeToStr Converts a variant value that stores a time to a string.

Furthermore, you can use string-handling routines that are provided by VBScript, JScript, C++Script, C#Script and DelphiScript engines.

Getting the string length

For example, the Utilities object does not have a method that calculates a string length, but every scripting language has it’s own routine that allows to do this.

  • In VBScript: Call the Len function to obtain the total number of characters in a string.
  • In JScript, C++Script, C#Script: Read the length property of the String object that corresponds to the desired string.
  • In DelphiScript: Call the Length function to obtain the total number of characters in a string.

    The character position in JScript, C++Script and C#Script is zero-based, so the maximum position number in a string is equal to the string length-1. In VBScript and DelphiScript the character position starts with 1, so the maximum position number in a string is equal to the string length.

    The following code prompts the user to input text and than posts the string length to TestComplete’s log:

    Concatenating strings

    The operation that forms a string out of several others is called concatenation. In JScript, C++Script and C#Script the String object has a special concat method that performs this operation. However, it is much more convenient to use the addition operator (+) for this purpose. In VBScript and DelphiScript the concatenation is only performed by the addition operator (+).

    The overloaded version of the plus operator works as follows: if one of the addition operands is a string, then the performed operation is considered to be a string concatenation rather than a number addition.

    This sample code shows how to make a string out of several substrings:

    The Utilities object has the an extended version of this operation StrLCat: additionally, concatenation allows you to set the maximal length of the resulting string.

    Accessing individual character of a string

    The string data type can hold as series of characters as well as individual characters. You can extract a single character out of the string.

    • In VBScript: Call the Mid function to get a substring of specified length.
    • In JScript, C++Script, C#Script: Call the charAt method of the String object that returns a letter at given position.
    • In DelphiScript: Strings are implemented as one-dimensional arrays, so to get the desired letter simply read the corresponding array element.

      The sample routine below illustrates this. It prompts the user to input text and than posts this text to TestComplete’s log in two different ways: as a whole string and by a single letter.


Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox