Mid, Mid$ Statement

See Also2ZXB0DW              Example1C5SGKL>Low

Replaces part of a string with another string.


Mid[$](stringvar, start [, length])  =  stringexpr


There is no functional difference between Mid and Mid$.  The Mid[$] statement has these parts:

Part                     Description


stringvar               String or Variant8PHEAW3 (VarType7A68ZTZ 8) variable to modify.

start                      Character position in stringvar where the replacement text begins.

length                   Number of characters to replace.

stringexpr             String expression1330R89 that replaces part of stringvar.


The arguments start and length must be between 1 and approximately 65,535, inclusive.  The argument stringvar must be a variable, but stringexpr can be any string expression.

The optional argument length refers to the number of characters from the argument stringexpr that are used in the replacement.  If length is omitted, all of stringexpr is used.  Whether or not length is included, the number of characters replaced is always less than or equal to the number of characters in stringvar.

See Also

Mid, Mid$ Function3XXKYW5

Right, Right$ FunctionQ1GJT8

Left, Left$ FunctionGU9X0I

Mid, Mid$ Statement Example

The example uses the Mid$ statement to substitute "dog" for  "fox" within a string.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Dim Head1, Head2, Orig, Start, Test, Msg ' Declare variables.

   Dim NL, TB

   NL = Chr(10): TB = Chr(9)                ' Define newline, tab.

   Head1 = "Original string:"

   Head2 = "Replacement string:"

   Orig = "The quick brown fox jumped over the wall."

   Test = Orig                              ' Make a copy of Original

   Start = Instr(Test,"fox")                ' Find where "fox" begins.

   Mid(Test, Start, 3) = "dog"              ' Replace "fox" with "dog".

   Msg = Head1 & NL & TB & Orig

   Msg = Msg & NL & Head2 & NL & TB & Test

   MsgBox Msg                               ' Display message

End Sub