Mid 陳述式

更新:2007 年 11 月

用另一字串的字元來取代 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 運算式。

例外狀況

例外狀況類型

錯誤代碼

條件

ArgumentException

5

Start <= 0 或 Length < 0。

如果將使用非結構化錯誤處理的 Visual Basic 6.0 應用程式升級,請參閱「錯誤代碼」資料行 (您可以將錯誤代碼與 Number 屬性 (Err 物件) 比對)。但是,請盡可能考慮以 Visual Basic 的結構化例外處理概觀 取代這類錯誤控制項。

備註

取代的字元數一律會小於或等於 Target 中的字元數。

Visual Basic 有 Mid 函式 (Visual Basic) 和 Mid 陳述式。雖然這兩個項目都是運作於字串中的指定字元數,但 Mid 函式會傳回字元,而 Mid 陳述式則會取代字元。如需詳細資訊,請參閱 Mid 函式 (Visual Basic)

注意事項:

在舊版的 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"

需求

命名空間 (Namespace)︰Microsoft.VisualBasic

模組 (Module):Strings

組件 (Assembly):Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

請參閱

參考

Mid 函式 (Visual Basic)

其他資源

Visual Basic 中的字串

Visual Basic 中的字串簡介