Strings.Mid Method

Definition

Returns a string that contains characters from a specified string.

Overloads

Mid(String, Int32)

Returns a string that contains all the characters starting from a specified position in a string.

Mid(String, Int32, Int32)

Returns a string that contains a specified number of characters starting from a specified position in a string.

Mid(String, Int32)

Returns a string that contains all the characters starting from a specified position in a string.

public:
 static System::String ^ Mid(System::String ^ str, int Start);
public static string? Mid (string? str, int Start);
public static string Mid (string str, int Start);
static member Mid : string * int -> string
Public Function Mid (str As String, Start As Integer) As String

Parameters

str
String

Required. String expression from which characters are returned.

Start
Int32

Required. Integer expression. Starting position of the characters to return. If Start is greater than the number of characters in str, the Mid function returns a zero-length string (""). Start is one-based.

Returns

String

A string that consists of all the characters starting from the specified position in the string.

Exceptions

Start <= 0 or Length < 0.

Examples

The last line in this example uses this overload of the Mid function to return the fifth and subsequent characters from a string.

' Creates text string.
Dim testString As String = "Mid Function Demo"
' Returns "Mid".
Dim firstWord As String = Mid(testString, 1, 3)
' Returns "Demo".
Dim lastWord As String = Mid(testString, 14, 4)
' Returns "Function Demo".
Dim midWords As String = Mid(testString, 5)

Remarks

To determine the number of characters in str, use the Len function.

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 Statement.

Note

The MidB function in previous versions of Visual Basic returns a string 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.

See also

Applies to

Mid(String, Int32, Int32)

Returns a string that contains a specified number of characters starting from a specified position in a string.

public:
 static System::String ^ Mid(System::String ^ str, int Start, int Length);
public static string Mid (string? str, int Start, int Length);
public static string Mid (string str, int Start, int Length);
static member Mid : string * int * int -> string
Public Function Mid (str As String, Start As Integer, Length As Integer) As String

Parameters

str
String

Required. String expression from which characters are returned.

Start
Int32

Required. Integer expression. Starting position of the characters to return. If Start is greater than the number of characters in str, the Mid function returns a zero-length string (""). Start is one based.

Length
Int32

Optional. Integer expression. Number of characters to return. If omitted or if there are fewer than Length characters in the text (including the character at position Start), all characters from the start position to the end of the string are returned.

Returns

String

A string that consists of the specified number of characters starting from the specified position in the string.

Exceptions

Start <= 0 or Length < 0.

Examples

The first two Mid functions in this example return the specified number of characters from a string, starting from the given positions. (The last function illustrates the Mid(String, Int32) overload and only specifies the starting point for the string extraction.)

' Creates text string.
Dim testString As String = "Mid Function Demo"
' Returns "Mid".
Dim firstWord As String = Mid(testString, 1, 3)
' Returns "Demo".
Dim lastWord As String = Mid(testString, 14, 4)
' Returns "Function Demo".
Dim midWords As String = Mid(testString, 5)

Remarks

To determine the number of characters in str, use the Len function.

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 Statement.

Note

The MidB function in previous versions of Visual Basic returns a string 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.

See also

Applies to