Mid 陳述式
將 String
變數中指定的字元數取代為另一個字串中的字元。
語法
Mid( _
ByRef Target As String, _
ByVal Start As Integer, _
Optional ByVal Length As Integer _
) = StringExpression
組件
Target
必要。 要修改的 String
變數名稱。
Start
必要。 Integer
運算式。 Target
中開始取代文字的字元位置。 Start
使用以一為基底的索引。
Length
選擇性。 Integer
運算式。 要取代的字元數。 如果省略,即會使用所有的 String
。
StringExpression
必要。 取代 Target
一部分的 String
運算式。
例外狀況
例外狀況類型 | Condition |
---|---|
ArgumentException | Start <= 0 或 Length < 0。 |
備註
取代的字元數一律小於或等於 Target
中的字元數。
Visual Basic 有 Mid 函式和 Mid
陳述式。 這些元素都會針對字串中指定的字元數運作,但 Mid
函式會傳回字元,而 Mid
陳述式會取代字元。 如需詳細資訊,請參閱Mid。
注意
舊版 Visual Basic 的 MidB
陳述式會以位元組取代子字串,而非字元。 其主要用於在雙位元組字元集 (DBCS) 應用程式中轉換字串。 所有 Visual Basic 字串都採用 Unicode,不再支援 MidB
。
範例
此範例使用 Mid
陳述式,將字串變數中指定的字元數取代為另一個字串中的字元。
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"
需求
命名空間: Microsoft.VisualBasic
模組:Strings
組件:Visual Basic 執行階段程式庫 (在 Microsoft.VisualBasic.dll 中)