Common VB functions in qtp

Source: Internet
Author: User
Tags rtrim alphanumeric characters

 

Left Function
Returns variant (string), which contains the specified number of characters from the left.
Syntax
Left (string, length)
The syntax of the Left function includes the following naming parameters:
 
Description
String required parameter. The leftmost characters in the string expression will be returned. If string contains null, null is returned.
Required parameter of length; it is variant (long ). A numeric expression that specifies the number of characters to be returned. If the value is 0, a zero-length string ("") is returned (""). If it is greater than or equal to the number of characters in a string, the entire string is returned.

Description
To learn the number of characters in string, use the Len function.
Note that the leftb function acts on the byte data contained in the string. Therefore, length specifies the number of bytes rather than the number of characters to be returned.

Mid Function
Returns a specified number of characters from a string.
Mid (string, start [, length])
Parameters
String
String expression, which returns characters. If string contains null, return null.
Start
The start position of the extracted characters in string. If start exceeds the number of characters in the string, Mid Returns a zero-length string ("").
Length
The number of characters to return. If the number of characters in the text is omitted or the length exceeds (including the characters at START), all characters from start to end of the string are returned.
Description
To determine the number of characters in a string, use the Len function.
The following example uses the mid function to return the six characters starting from the fourth character in the string:
Dim myvar
Myvar = mid ("VB script is fun! ", 4, 6) 'myvar contains" script ".
Note that the midb function is used with the byte data contained in the string. Its parameter is not the specified number of characters, but the number of bytes.

Len Function
Returns the number of characters in a string or the number of bytes required to store a variable.
Len (string | varname)
Parameters
String
Any valid string expression. If the string parameter contains null, null is returned.
Varname
Any valid variable name. If the varname parameter contains null, null is returned.
Description
The following example uses the Len function to return the number of characters in a string:
Dim mystring
Mystring = Len ("VBScript") 'mystring contains 8.
Note that the lenb function is used with the byte data contained in the string. Lenb does not return the number of characters in a string, but returns the number of bytes used to represent the string.

Right function
Returns a specified number of characters from the right of the string.
Right (string, length)
Parameters
String
String expression. the rightmost character is returned. If the string parameter contains null, null is returned.
Length
A numeric expression that specifies the number of characters to return. If the value is 0, a zero-length string is returned. If the value is greater than or equal to the number of all characters in the string parameter, the entire string is returned.
Description
To determine the number of characters in the string parameter, use the Len function.
The following example uses the right function to return a specified number of characters from the right side of the string:
Dim anystring, mystr
Anystring = "Hello World" 'defines a string.
Mystr = right (anystring, 1) 'returns "D ".
Mystr = right (anystring, 6) 'returns "world ".
Mystr = right (anystring, 20) 'returns "Hello World ".
Note that the rightb function is used to specify the number of bytes returned by the length parameter, rather than the number of characters.

Instr Functions
Returns the position where a string appears for the first time in another string.
Instr ([start,] string1, string2 [, compare])
Parameters
Start
Optional. Value expression, used to set the start position of each search. If omitted, the search starts from the first character. If start contains null, an error occurs. If the compare parameter is specified, the start parameter is required.
String1
Required. String expression to be searched.
String2
Required. The string expression to be searched.
Compare
Optional. Indicates the value of the comparison type used to calculate the substring. For values, see the "Settings" section. If omitted, binary comparison is executed.
Set
The compare parameter can have the following values:
 
Constant Value description
Vbbinarycompare 0 performs binary comparison.
Vbtextcompare 1 performs text comparison.
Return Value
The instr function returns the following values:
 
If instr returns
String1 0 length 0
String1 is null
String2 zero-length start
String2 is null
String2 does not find 0
Find string2 in string1 and find the position matching the string
Start> Len (string2) 0
Description
The following example uses instr to search for strings:
Dim searchstring, searchchar, Mypos
Searchstring = "xxpxxpxxpxxp" ': the string to be searched.
Searchchar = "p" 'search for "p ".
Mypos = instr (4, searchstring, searchchar, 1) 'text comparison at location 4. Returns 6.
Mypos = instr (1, searchstring, searchchar, 0) 'binary comparison at position 1. Return 9.
Mypos = instr (searchstring, searchchar) 'by default, binary comparison is performed (the final parameter is omitted ). Return 9.
Mypos = instr (1, searchstring, "W") 'binary comparison at position 1. 0 is returned ("W" is not found ").
Note that the instrb function uses byte data contained in the string. Therefore, the returned value of instrb is not the first occurrence of a character string in another string, but the byte position.

Ltrim, rtrim, and trim Functions
Returns a string copy that does not contain leading spaces (ltrim), subsequent spaces (rtrim), or leading and subsequent spaces (TRIM.
Ltrim (string)
Rtrim (string)
Trim (string)
The string parameter is any valid string expression. If the string parameter contains null, null is returned.
Description
The following example uses the ltrim, rtrim, and trim functions to remove the leading space, trailing space, starting space, and trailing space of a string:
Dim myvar
Myvar = ltrim ("VBScript") 'myvar contains "VBScript ".
Myvar = rtrim ("VBScript") 'myvar contains "VBScript ".
Myvar = trim ("VBScript") 'myvar contains "VBScript ".

RND Function
Returns a random number.
RND [(number)]
The number parameter can be any valid numeric expression.
Description
The RND function returns a value smaller than 1 but greater than or equal to 0. The value of number determines how RND generates a random number:
 
If number is generated by RND
If the value is smaller than zero, the same value is used every time. The number is used as the seed.
The value is greater than the next random number in the zero sequence.
It is equal to the number of nearest generation.
The next random number in the sequence is omitted.
Because the previous number in the sequence is used as the seed of the next number each time the RND function is called consecutively, the same sequence is generated for any initial given seed.
Before calling RND, initialize the random number generator using the unparametric randomize statement, which has a seed based on the system timer.
To generate a random integer in the specified range, use the following formula:
INT (upperbound-lowerbound + 1) * RND + lowerbound)
Here, upperbound is the upper bound of this range, while lowerbound is the lower bound of this range.
Note that the sequence of random numbers must be repeated. before using the numeric parameter to call randomize, call RND immediately with the negative value parameter. Randomize with the same number value cannot repeat the previous random number sequence.

Randomize statement
Initialize the random number generator.
Syntax
Randomize [number]
The optional number parameter is variant or any valid numeric expression.
Description
Randomize initializes the RND function's random number generator with number, which gives number a new seed value. If number is omitted, the value returned by the system timer is used as the new seed value.
If randomize is not used, the RND function uses the seed value of the first RND function call.
Note: If you want to obtain a recurring random number sequence, call RND with a negative parameter value before using randomize with a numeric parameter. Using randomize with the same number value will not produce repeated random number sequences.
RND function example
In this example, a random integer ranging from 1 to 6 is randomly generated using the RND function.
In this example, the random number generator is initialized using the randomize statement. Because the numeric parameter is ignored, randomize uses the return value of the timer function as the seed value of the new random number.

Dim myvalue
Randomize' initializes the random number generator.

Myvalue = int (6 * RND) + 1) 'generates a random value between 1 and 6.
Split Function
Description
Returns a one-dimensional array with a subscript starting from zero. It contains a specified number of substrings.
Syntax
Split (expression [, delimiter [, Count [, compare])
The split function syntax includes the following parts:
 
Partial description
Expression is required. A string expression that contains substrings and delimiters. If expression is a zero-length string (""), split returns an empty array with no elements or data.
Optional. String character used to identify the substring boundary. If ignored, the space character ("") is used as the separator. If Delimiter is a zero-length string, the returned array contains only one element, that is, the complete expression string.
Optional. The number of substrings to return.-1 indicates that all substrings are returned.
Compare is optional. Numeric value, indicating the comparison method used to identify a substring. For more information about the value, see "set value.

Set Value
The compare parameter settings are as follows:
 
Constant Value description
Vbusecompareoption-1 is compared with the set value in the option compare statement.
Vbbinarycompare 0 performs binary comparison.
Vbtextcompare 1.
Vbdatabasecompare 2 is only used for Microsoft Access. Perform comparison based on your database information.

Replace Function
Return string, where a specified number of substrings is replaced with another substring.
Replace (expression, find, replacewith [, compare [, Count [, start])
Parameters
Expression
Required. The string expression contains the substring to be replaced.
Find
Required. The substring to be searched.
Replacewith
Required. The substring used for replacement.
Start
Optional. Expression to start searching for the position of the substring. If omitted, the default value is 1. Must be used when associating with Count
Count
Optional. Number of substrings to replace. If this parameter is omitted, the default value is-1, indicating all possible replacements. It must be used in association with start.
Compare
Optional. Indicates the value of the comparison type used to calculate the substring. For values, see the "Settings" section. If omitted, the default value is 0, which means binary comparison is required.
Set
The compare parameter can have the following values:
 
Constant Value description
Vbbinarycompare 0 performs binary comparison.
Vbtextcompare 1 performs text comparison.
Return Value
Replace returns the following values:
 
If replace returns
Expression is a zero-length, zero-length string ("").
Expression is null.
Find a zero-length Expression copy.
Replacewith is a zero-length Expression copy, in which all content specified by the find parameter is deleted.
Start> Len (expression) Zero-length string.
Count is a copy of 0 expression.
Description
The Return Value of the replace function is a string after replacement (starting from the position specified by start to the end of the expression string), rather than a copy of the original string from start to end.
The following example uses the replace function to return a string:
Dim mystring
Mystring = Replace ("xxpxxpxxp", "P", "Y") 'binary comparison starts from the left end of the string. Returns "xxyxxpxxy ".
Mystring = Replace ("xxpxxpxxp", "P", "Y", 'text comparison starts with the third character. Returns "yxxyxxy ". 3,-1, 1)

Strcomp Function
Returns a value indicating the result of string comparison.
Strcomp (string1, string2 [, compare])
Parameters
String1
Required. Any valid string expression.
String2
Required. Any valid string expression.
Compare
Optional. Indicates the numeric value of the comparison type used for string calculation. If this parameter is omitted, binary comparison is executed. For values, see the "Settings" section.
Set
The compare parameter can have the following values:
 
Constant Value description
Vbbinarycompare 0 performs binary comparison.
Vbtextcompare 1 performs text comparison.
Return Value
The strcomp function returns the following values:
 
If strcomp returns
String1 is less than string2-1
String1 equals to string2 0
String1 is greater than string2 1
String1 or string2 is null
Description
The following example uses the strcomp function to return the result of string comparison. If the third parameter is 1, text comparison is executed. If the third parameter is 0, binary comparison is executed.
Dim mystr1, mystr2, mycomp
Mystr1 = "ABCD": mystr2 = "ABCD" 'defines the variable.
Mycomp = strcomp (mystr1, mystr2, 1) 'returns 0.
Mycomp = strcomp (mystr1, mystr2, 0) 'returns-1.
Mycomp = strcomp (mystr2, mystr1) 'returns 1.

CINT Function
Returns an expression that has been converted to a variant of the integer subtype.
CINT (expression)
The expression parameter is any valid expression.
Description
Generally, code can be written using a subtype Conversion Function to show that the results of some operations should be represented as specific data types, rather than the default type. For example, in the case of currency, single-precision, or double-precision operations, use CINT or clng to force integer operations.
The CINT function is used to convert data from other data types to integer subtypes in internationally recognized formats. For example, the identification of a decimal separator (for example, a kilobytes) may depend on the system's regional settings.
An error occurs if expression is out of the acceptable range of integer subtypes.
The following example uses the CINT function to convert a value to an integer:
Dim mydouble, Myint
Mydouble = 2345.5678 'mydouble is double.
Myint = CINT (mydouble) 'myint contains 2346.
Note that CINT is different from the fix and INT functions to delete the decimal part of a value. Instead, it is rounded down. When the decimal part is equal to 0.5, The CINT always rounds it to an even number closest to the number. For example, 0.5 rounded to 0, and 1.5 rounded to 2.

CSTR Function
Returns an expression that has been converted to a string-type variant.
CSTR (expression)
The expression parameter is any valid expression.
Description
Generally, code can be written using a subtype Conversion Function to show that the results of some operations should be represented as specific data types, rather than the default type. For example, use CSTR to forcibly represent the result as a string.
The CSTR function is used to replace the STR function for internationally recognized format conversion from other data types to string subtypes. For example, the identification of the decimal separator depends on the system's region settings.
The expression determines the returned data according to the following table:
 
If expression is CSTR
Boolean string, including true or false.
Date string, which contains the system's short date format date.
Null runtime error.
Empty zero-length string ("").
Error string, which contains the word "error" that follows the wrong number.
Other numeric strings, including this number.
The following example uses the CSTR function to convert a number to a string:
Dim mydouble, mystring
Mydouble = 437.324 'mydouble is a double value.
Mystring = CSTR (mydouble) 'mystring contains "437.324 ".

Lcase Functions
Returns the string in lowercase.
Lcase (string)
The string parameter is any valid string expression. If the string parameter contains null, null is returned.
Description
Only uppercase letters are converted to lowercase letters. All lowercase letters and non-alphanumeric characters remain unchanged.

The following example uses the lcase function to convert uppercase letters to lowercase letters:
Dim mystring
Dim lcasestring
Mystring = "VBScript"
Lcasestring = lcase (mystring) 'lcasestring contains "VBScript ".
Ucase Functions
Returns the string in uppercase.
Ucase (string)
The string parameter is any valid string expression. If the string parameter contains null, null is returned.
Description
Only lowercase letters are converted to uppercase letters. all uppercase letters and non-alphanumeric characters remain unchanged.
The following example uses the ucase function to return the string in uppercase:
Dim myword
Myword = ucase ("Hello World") 'returns "Hello World ".

Msgbox Function
The message is displayed in the dialog box. Wait for the user to click the button and return a value indicating the button clicked by the user.
Msgbox (prompt [, Buttons] [, title] [, helpfile, context])
Parameters
Prompt
It is a string expression displayed in the dialog box. The maximum length of a prompt is about 1024 characters, depending on the width of the character used. If the prompt contains multiple rows, you can use a combination of carriage return (CHR (13), line feed (CHR (10), or carriage return linefeed (CHR (13) between each row) & CHR (10) is used to separate rows.
Buttons
A numeric expression indicates the number and type of buttons displayed, the icon style used, the identifier of the default button, and the total value of the message box style. For values, see the "Settings" section. If this parameter is omitted, the default value of buttons is 0.
Title
The string expression displayed in the title bar of the dialog box. If title is omitted, the application name is displayed in the title bar.
Helpfile
String expression used to identify the Help file that provides context-related help in the dialog box. If helpfile is provided, context must be provided. Unavailable on a 16-bit system platform.
Context
A numeric expression used to identify the context number specified by the creator of the Help file to a help topic. If context is provided, helpfile must be provided. Unavailable on a 16-bit system platform.
Set
The buttons parameter can have the following values:
 
Constant Value description
Vbokonly 0 only displays the OK button.
Vbokcancel 1 displays the OK and cancel buttons.
Vbabortretryignore 2 displays the discard, retry, and ignore buttons.
Vbyesnocancel 3 displays the "yes", "no", and "cancel" buttons.
Vbyesno 4 displays the "yes" and "no" buttons.
Vbretrycancel 5 displays the retry and cancel buttons.
Vbcritical 16 displays the critical information icon.
Vbquestion 32 displays the alarm query icon.
Vbexclamation 48 displays the warning message icon.
Vbinformation 64 displays the message icon.
Vbdefaultbutton1 0 the first button is the default button.
Vbdefaultbutton2 256 the second button is the default button.
Vbdefaultbutton3 512 the third button is the default button.
Vbdefaultbutton4 768 the fourth button is the default button.
Vbapplicationmodal 0 Application Mode: You must respond to the message box to continue working in the current application.
Vbsystemmodal 4096 system mode: All applications are suspended before the user responds to the message box.
The first value (0-5) is used to describe the type and number of buttons displayed in the dialog box. The second value (16, 32, 48, 64) is used to describe the icon style; the third value (0,256,512) is used to determine the default button, while the fourth value (0, 4096) determines the message frame style. When these numbers are used to generate the buttons parameter value, only one number can be used from each group of values.
Return Value
Msgbox functions return the following values:
 
Constant Value button
Vbok 1 OK
Vbcancel 2 canceled
Vbabort 3 abandon
Vbretry 4 retry
Vbignore 5 ignore
Vbyes 6 is
Vbno 7 No
Description
If both helpfile and context are provided, you can press F1 to view the help topic corresponding to the context.
If the cancel button is displayed in the dialog box, Press ESC and click Cancel. If the dialog box contains the Help button, context-related help is provided for the dialog box. However, no value is returned before other buttons are clicked.
When Microsoft Internet Explorer uses the msgbox function, the title of any Dialog Box always includes "VBScript" to distinguish it from the standard dialog box.
The following example demonstrates the usage of the msgbox function:
Dim myvar
Myvar = msgbox ("Hello world! ", 65," msgbox example ")
'Myvar contains 1 or 2, depending on which button is clicked.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.