Q(uick)BASIC Function: MID$

Quick View

MID$

A string-processing function that returns a substring of a string

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • MID$(stringexpression$,start%[,length%])
  • MID$(stringvariable$,start%[,length%])=stringexpression$
Description/Parameter(s)
stringexpression$ The string from which the MID$ function returns a substring, or the replacement string used by the MID$ statement. It can be any string expression.
start% The position of the first character in the substring being returned or replaced.
length% The number of characters in the substring. If the length is omitted, MID$ returns or replaces all characters to the right of the start position.
stringvariable$ The string variable being modified by the MID$ statement.
Example
a$ = "Where is Paris?" PRINT MID$(a$, 10, 5) 'Output is: Paris Text$ = "Paris, France" PRINT Text$ 'Output is: Paris, France MID$(Text$, 8) = "Texas " PRINT Text$ 'Output is: Paris, Texas
Syntax
  • MID$(stringexpression,start[,length])
Description/Parameter(s)
Argument Description
stringexpression The string expression that the substring is extracted from. This can be any string expression.
start The character position in stringexpression where the substring starts.
length The number of characters to extract.

The arguments start and length must be in the range 1 to 32,767. If length is omitted or if there are fewer than length characters to the right of the start character, the MID$ function returns all characters to the right of the start character.

If start is greater than the number of characters in stringexpression, MID$ returns a null string.

Use the LEN function to find the number of characters in stringexpression.

Example

This example converts a binary number to a decimal number. The program uses the MID$ function to extract digits from the binary number (input as a string).

CLS ' Clear screen INPUT "Binary number = ", Binary$ 'Input binary number as 'string. Length = LEN(Binary$) 'Get length of string. Decimal = 0 FOR K = 1 TO Length 'Get individual digits from string, from left to right. Digit$ = MID$(Binary$, K, 1) 'Test for valid binary digit. IF Digit$ = "0" OR Digit$ = "1" THEN 'Convert digit characters to numbers. Decimal = 2 * Decimal + VAL(Digit$) ELSE PRINT "Error--invalid binary digit: "; Digit$ EXIT FOR END IF NEXT PRINT "Decimal number =" Decimal

Sample Output:

Binary number = 10110 Decimal number = 22
Syntax
  • MID$(stringexpression$,start%[,length%])
Description/Parameter(s)
stringexpression$ The string from which the substring is to be extracted. This can be any string expression.
start% The character position in stringexpression$ where the substring starts.
length% The number of characters to extract. This can be omitted if you want all the characters to the right of start%.
  • The arguments start% and length% must be numeric expressions between 1 and 32,767, inclusive.
  • If length% is omitted or if there are fewer than length% characters in the string (including the start character), the MID$ function returns all characters from the position start% to the end of the string.
  • If start% is greater than the number of characters in stringexpression$, MID$ returns a null string.
  • Use the LEN function to find the number of characters in stringexpression$.
Example

This example uses the MID$ function to extract digits from the binary number (input as a string) during the conversion of a binary number to a decimal number.

CLS 'Clear screen. INPUT "Binary number = ", Binary$ 'Input binary number as string. Length = LEN(Binary$) 'Get length of string. Decimal = 0 FOR K = 1 TO Length 'Get individual digits from string, from left to right. Digit$ = MID$(Binary$, K, 1) 'Test for valid binary digit. IF Digit$ = "0" OR Digit$ = "1" THEN 'Convert digit characters to numbers. Decimal = 2 * Decimal + VAL(Digit$) ELSE PRINT "Error--invalid binary digit: "; Digit$ EXIT FOR END IF NEXT PRINT "Decimal number ="; Decimal

Sample Output:

Binary number = 10110 Decimal number = 22