Partilhar via


Instrução MID

Replaces a specified number of characters in a String variable with characters from another string.

Mid( _
   ByRef Target As String, _
   ByVal Start As Integer, _
   Optional ByVal Length As Integer _
) = StringExpression

Parts

  • Target
    Required. Name of the String variable to modify.

  • Start
    Required. Integerexpressão. Posição de caractere Target onde a substituição de texto começa. Startusa a um-, com base de índice.

  • Length
    Optional. Integerexpressão. Number of characters to replace. If omitted, all of String is used.

  • StringExpression
    Required. Stringexpressão substitui parte de Target.

Exceções

Exception type

Error number

Condition

ArgumentException

5

Start<= 0 or Length < 0.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (Você pode comparar o número do erro contra a Number propriedade.) However, when possible, you should consider replacing such error control with Visão Geral sobre a Manipulação Estruturada de Exceções para o Visual Basic.

Comentários

The number of characters replaced is always less than or equal to the number of characters in Target.

Visual Basic has a Mid function and a Mid statement. These elements both operate on a specified number of characters in a string, but the Mid function returns the characters while the Mid statement replaces the characters. For more information, see Mid.

ObservaçãoObservação

The MidB statement of earlier versions of Visual Basic replaces a substring in bytes, rather than characters. It is used primarily for converting strings in double-byte character set (DBCS) applications. All Visual Basic strings are in Unicode, and MidB is no longer supported.

Exemplo

This example uses the Mid statement to replace a specified number of characters in a string variable with characters from another string.

Dim TestString As String
' Initializes string.
TestString = "The dog jumps"
' Returns "The fox jumps".
Mid(TestString, 5, 3) = "fox"
' Returns "The cow jumps".
Mid(TestString, 5) = "cow"
' Returns "The cow jumpe".
Mid(TestString, 5) = "cow jumped over"
' Returns "The duc jumpe".
Mid(TestString, 5, 3) = "duck"

Requisitos

Namespace: Microsoft.VisualBasic

Módulo: Strings

Assembly: Visual Basic Runtime Library (em Microsoft.VisualBasic.dll)

Consulte também

Referência

Mid

Outros recursos

Sequências de caracteres em Visual Basic

Introdução a sequências de caracteres no Visual Basic