STRING Data Type

Description

A variable of type STRING stores character strings. You define a string by enclosing it in either single (') or double quotation marks (").

For the range of valid values, see Data type ranges.

To sort lists of strings and compare strings by ASCII value, add the line AsciiSort=TRUE in the [Runtime] section of the partner.ini file. This setting also affects the comparison of strings, not just sorting of lists of strings.

Including Quotation Marks in Strings

To include a quotation mark as part of a string delimited by the same kind of quotation mark, "escape" it by typing two quotation marks in succession:

  • "This string contains a double quotation mark " " ."
  • "This string contains a single quotation mark '."

Evaluating in-string Expressions

4Test can evaluate an expression within a quoted string, and convert its value to the string representation of the value. This feature provides an alternative to using lengthy sequences of string concatenation in order to put values into strings.

To have 4Test evaluate an expression inside a string, enclose it in braces ({}). The braces act as a typecast operator to explicitly cast the result of the evaluated expression as a string. To include literal braces in a quoted string, you need to escape each open brace using either the single quotation mark or the double quotation mark, whichever you used to delimit the string. See the first example below.

Accessing Characters in Strings

4Test lets you get or replace the individual characters in strings, as if they were one-dimensional arrays of characters. See the second example below.

Examples

[ ] // Example of evaluating in-string expressions
[-] type NAME is record
	[ ] STRING sFirst 
	[ ] STRING sLast 
[-] testcase String1 ()
	[ ] INTEGER iTestNum = 1, iNum = 2
	[ ] STRING sTest = "TestMenu" 
	[ ] // Automatically converts list "Name" to a record 
	[-] NAME Name = {...}
		[ ] "Beenie" 
		[ ] "Cecil" 
	[ ] Print ("Test number: {iTestNum} - {sTest}") 
	[ ] Print ("Tester: {Name.sFirst} {Name.sLast}") 
	[ ] Print ("Square ({iNum}) = {Square(iNum)}") 
	[ ] Print ("String with "{braces} in it") 
[-] INTEGER Square (INTEGER I)
	[ ] return I * I 
[ ] // This script prints:
[ ] // Test number: 1 - TestMenu
[ ] // Tester: Beenie Cecil
[ ] // Square(2) = 4
[ ] // String with {braces} in it
[ ] 
[ ] // Example of accessing characters in strings
[-] testcase String2 ()
	[ ] STRING sfruit = "peach"
	[ ] Print (sfruit[3]) // Prints 'a'
	[ ] sfruit[2] = 'o'
	[ ] Print (sfruit) // Prints "poach"
	[ ] // You can also delete, insert, or replace characters: 
	[ ] sfruit = "pear" 
	[ ] sfruit[3] = ""
	[ ] Print (sfruit) // Prints "per"
	[ ] sfruit[3] = "ace"  
	[ ] Print (sfruit) // Prints "peace"
	[ ] // You can index one character beyond the end
	[ ] // of the string; result is an empty string (""). 
	[ ] // If you attempt to go more than one character  
	[ ] // beyond the end of the string, 4Test raises an 
	[ ] // exception. For example:
	[ ] sfruit = "orange" 
	[ ] Print (sfruit[7]) // Prints "" 
	[ ] sfruit[7] = "s"
	[ ] Print (sfruit) // Prints "oranges"