Strings.Mid 方法

定義

傳回包含指定之字串中的字元的字串。

多載

Mid(String, Int32)

傳回字串,這個字串包含某個字串中從指定之位置開始的所有字元。

Mid(String, Int32, Int32)

傳回字串,這個字串包含某個字串中從指定之位置開始的指定數目字元。

Mid(String, Int32)

來源:
Strings.vb
來源:
Strings.vb
來源:
Strings.vb

傳回字串,這個字串包含某個字串中從指定之位置開始的所有字元。

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

參數

str
String

必要。 String 運算式,字元會從此運算式中傳回。

Start
Int32

必要。 Integer 運算式。 要傳回字元的開始位置。 如果 Start 大於 str 中的字元數,則 Mid 函式會傳回長度為零的字串 (""); Start 是以一起始。

傳回

字串,包含字串中從指定之位置開始的所有字元。

例外狀況

Start<= 0 或 Length< 0。

範例

此範例中的最後一行會使用此函式的多 Mid 載傳回字串中的第五個和後續字元。

' 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)

備註

若要判斷 中的 str字元數,請使用 函 Len 式。

Visual Basic 有 Mid 函式和 Mid 陳述式。 這些元素都會針對字串中指定的字元數運作,但 Mid 函式會傳回字元,而 Mid 陳述式會取代字元。 如需詳細資訊,請參閱 Mid 語句

注意

舊版 Visual Basic 中的函 MidB 式會以位元組而非字元傳回字串。 其主要用於在雙位元組字元集 (DBCS) 應用程式中轉換字串。 所有 Visual Basic 字串都採用 Unicode,不再支援 MidB

另請參閱

適用於

Mid(String, Int32, Int32)

來源:
Strings.vb
來源:
Strings.vb
來源:
Strings.vb

傳回字串,這個字串包含某個字串中從指定之位置開始的指定數目字元。

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

參數

str
String

必要。 String 運算式,字元會從此運算式中傳回。

Start
Int32

必要。 Integer 運算式。 要傳回字元的開始位置。 如果 Start 大於 str 中的字元數,則 Mid 函式會傳回長度為零的字串 (""); Start 是以一起始。

Length
Int32

選擇性。 Integer 運算式。 要傳回的字元數。 如果省略,或者此字元數少於文字中的 Length 字元 (包括 Start 位置上的字元),則會傳回從起始位置到字串結尾的所有字元。

傳回

字串,包含字串中從指定之位置開始的指定數目字元。

例外狀況

Start<= 0 或 Length< 0。

範例

本範例中的前兩 Mid 個函式會從指定位置開始傳回字串中的指定字元數。 (最後一個函式說明 Mid(String, Int32) 多載,並且只指定字串擷取的起點。)

' 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)

備註

若要判斷 中的 str字元數,請使用 函 Len 式。

Visual Basic 有 Mid 函式和 Mid 陳述式。 這些元素都會針對字串中指定的字元數運作,但 Mid 函式會傳回字元,而 Mid 陳述式會取代字元。 如需詳細資訊,請參閱 Mid 語句

注意

舊版 Visual Basic 中的函 MidB 式會以位元組而非字元傳回字串。 其主要用於在雙位元組字元集 (DBCS) 應用程式中轉換字串。 所有 Visual Basic 字串都採用 Unicode,不再支援 MidB

另請參閱

適用於