Strings.Mid 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳包含指定字串字元的字串。
多載
| 名稱 | Description |
|---|---|
| Mid(String, Int32) |
回傳一個字串,包含從字串中指定位置開始的所有字元。 |
| Mid(String, Int32, Int32) |
回傳一個包含指定字元數量、從字串中指定位置開始的字串。 |
Mid(String, Int32)
回傳一個字串,包含從字串中指定位置開始的所有字元。
public:
static System::String ^ Mid(System::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 陳述式會取代字元。 欲了解更多資訊,請參閱 中期聲明。
備註
Visual Basic 早期版本中的 MidB 函式會回傳以位元組為單位的字串,而非字元。 其主要用於在雙位元組字元集 (DBCS) 應用程式中轉換字串。 所有 Visual Basic 字串都採用 Unicode,不再支援 MidB。
另請參閱
- Left(String, Int32)
- Len
- Trim(String)
- LTrim(String)
- RTrim(String)
- Right(String, Int32)
- ArgumentException
- 字串操作摘要
弦在Visual Basic - Visual Basic 中的字串簡介
- 中期陳述
適用於
Mid(String, Int32, Int32)
回傳一個包含指定字元數量、從字串中指定位置開始的字串。
public:
static System::String ^ Mid(System::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
Optional.
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 陳述式會取代字元。 欲了解更多資訊,請參閱 中期聲明。
備註
Visual Basic 早期版本中的 MidB 函式會回傳以位元組為單位的字串,而非字元。 其主要用於在雙位元組字元集 (DBCS) 應用程式中轉換字串。 所有 Visual Basic 字串都採用 Unicode,不再支援 MidB。
另請參閱
- Left(String, Int32)
- Len
- Trim(String)
- LTrim(String)
- RTrim(String)
- Right(String, Int32)
- ArgumentException
- 字串操作摘要
弦在Visual Basic - Visual Basic 中的字串簡介
- 中期陳述