Q(uick)BASIC Statement: MID$

Quick View

MID$

A string processing statement that replaces a portion of a string variable with another 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$(stringvariable,start[,length])=stringexpression
Description/Parameter(s)
Argument Description
stringvariable The string variable being modified.
start A numeric expression giving the position in stringvariable where the replacement starts.
length The length of the string being replaced. The length is a numeric expression.
stringexpression The string expression that replaces part of the stringvariable.

The arguments start and length are integer expressions. The argument stringvariable is a string variable, but stringexpression can be a string variable, string constant, or string expression.

The optional length refers to the number of characters from the argument stringexpression that are used in the replacement. If length is omitted, all of stringexpression is used. However, regardless of whether length is omitted or included, the replacement of characters never goes beyond the original length of stringvariable.

Example

This example uses the MID$ statement to replace string characters.

CLS ' Clear screen Test$ = "Paris, France" PRINT Test$ MID$(Test$, 8)="Texas " ' Starting at position 8, replace ' characters in Test$ with Texas PRINT Test$

Sample Output:

Paris, France Paris, Texas
Syntax
  • MID$(stringvariable$,start%[,length%])=stringexpression$
Description/Parameter(s)
stringvariable$ The string variable being modified.
start% The character position at which the change starts.
length% If included, the number of characters being replaced. The length% is a numeric expression.
stringexpression$ The new characters.

Usage Notes

  • The optional length refers to the number of characters from the argument stringexpression$ that are used in the replacement. If length% is omitted, all of stringexpression$ is used. However, regardless of whether length% is omitted or included, the replacement of characters never goes beyond the original length of stringvariable$.
Example

This example uses the MID$ statement to replace string characters.

CLS 'Clear screen. Test$ = "Paris, France" PRINT Test$ MID$(Test$, 8) = "Texas " 'Starting at position 8, replace 'characters in Test$ with Texas. PRINT Test$

Sample Output:

Paris, France Paris, Texas