41
4.4.5 RETRIEVING PART OF STRINGS
Several commands are provided to take strings apart by returning pieces of a string,
from the left side, or the right side, or the middle of the target string.
LEFT$
Purpose To retrieve a given number of characters from the left side of the target string.
Syntax A$ = LEFT$(X$, N%)
Remarks “A$” is a string variable to be assigned to the result.
“X$” may be a string variable, string expression, or string constant.
“N%” is a numeric expression in the range of 0 to 255.
If N is larger than the length of X$, the entire string (X$) is returned.
If N is zero, the null string (with length 0) is returned.
Example
String1$ = “11025John Thomas, Accounting Manager”
EmployeeID$ = LEFT$(String1$, 5)
MID$
Purpose To retrieve a given number of characters from anywhere of the target string.
Syntax A$ = MID$(X$, N%[, M%])
Remarks “A$” is a string variable to be assigned to the result.
“X$” may be a string variable, string expression, or string constant.
“N%” and “M%” are numeric expressions in the range of 0 to 255.
This command returns a string of length M characters from X$
the Nth character.
If M is omitted, or if there are fewer than M
characters to the right of the
Nth character, all the characters beginning with the N
rightmost are returned.
If M is equal to zero, or if N is greater than the length of X$
returns a null string.
Example
String1$ = “11025John Thomas, Accounting Manager”
String2$ = “,”
EmployeeName$ = MID$(String1$, 6, INSTR(String1$, String2$) – 6)
‘ the employee’s name starts at the sixth character