Mid PowerScript function
Description
Obtains a specified number of characters from a specified
position in a string.
Syntax
1 |
<span>Mid</span> ( <span>string</span>, <span>start</span> {, <span>length</span> } ) |
Argument |
Description |
---|---|
string |
The string from which you want characters |
start |
A long specifying the position of the |
length (optional) |
A long whose value is the number of characters |
Return Values
String. Returns characters specified
in length of string starting
at character start. If start is
greater than the number of characters in string,
the Mid function returns the empty string (“”).
If length is greater than the number of characters remaining
after the start character, Mid returns
the remaining characters. The return string is not filled with spaces
to make it the specified length. If any argument’s value
is null, Mid returns null.
Usage
To search a string for the position of the substring that
you want to extract, use the Pos function. Use
the return value for the start argument of Mid.
To extract a specified number of characters from the beginning or
end of a string, use the Left or the Right function.
Examples
This statement returns RUTH:
1 |
<span>Mid</span>("BABE RUTH", 5, 5) |
This statement returns “”:
1 |
<span>Mid</span>("BABE RUTH", 40, 5) |
This statement returns BE RUTH:
1 |
<span>Mid</span>("BABE RUTH", 3) |
These statements store the characters in the SingleLineEdit sle_address from the
40th character to the end in ls_address_extra:
1 |
string ls_address_extra |
1 |
ls_address_extra = <span>Mid</span>(sle_address.Text, 40) |
The following user-defined function, called str_to_int_array,
converts a string into an array of integers. Each integer in the
array will contain two characters (one characters as the high byte
(ASCII value * 256) and the second character as the low
byte). The function arguments are str, a string
passed by value, and iarr, an integer array
passed by reference. The length of the array is initialized before
the function is called. If the integer array is longer than the
string, the script stores spaces. If the string is longer, the script
ignores the extra characters.
To call the function, use code like the following:
1 |
int rtn |
1 |
iarr[20]=0// Initialize the array, if necessary |
1 |
rtn = str_to_int_array("This is a test.", iarr) |
The str_to_int_array function
is:
1 |
long stringlen, arraylen, i |
1 |
string char1, char2 |
1 |
1 |
// Get the string and array lengths |
1 |
arraylen = UpperBound(iarr) |
1 |
stringlen = Len(str) |
1 |
1 |
// Loop through the array |
1 |
FOR i = 1 to arraylen |
1 |
IF (i*2 <= stringlen) THEN |
1 |
// Get two chars from str |
1 |
char1 = <span>Mid</span>(str, i*2, 1) |
1 |
char2 = <span>Mid</span>(str, i*2 - 1, 1) |
1 |
ELSEIF (i*2 - 1 <= stringlen) THEN |
1 |
// Get the last char |
1 |
char1 = " " |
1 |
char2 = <span>Mid</span>(str, i*2 - 1, 1) |
1 |
ELSE |
1 |
// Use spaces if beyond the end of str |
1 |
char1 = " " |
1 |
char2 = " " |
1 |
END IF |
1 |
iarr[i] = Asc(char1) * 256 + Asc(char2) |
1 |
NEXT |
1 |
RETURN 1 |
For sample code that converts the integer array back to a
string, see Asc.
See Also
-
AscA, Left, Pos, Right, UpperBound, Mid method
for DataWindows in the DataWindow Reference or the online Help