Strings.Left(String, Int32) 方法

定義

傳回字串,其中包含從字串的左邊開始的指定數目的字元。

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

參數

str
String

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

Length
Int32

必要。 Integer 運算式。 數值運算式,表示要傳回的字元數。 如果為 0,則會傳回長度為零的字串 ("")。 如果大於或等於 str 中的字元數,則會傳回整個字串。

傳回

字串,其中包含從字串的左邊開始的指定數目的字元。

例外狀況

Length< 0.

範例

此範例示範如何使用 Left 函式傳回指定 String的子字串。 在具有 屬性的 Left 類別中,可能需要完整限定函 Left 式。

Dim testString As String = "Hello World!"
' Returns "Hello".
Dim subString As String = Left(testString, 5)

備註

若要判斷 中的 str字元數,請使用 函 Len 式。 如果在 Windows Forms 應用程式中使用,或任何其他具有 屬性的Left類別,您必須使用 完整限定函Microsoft.VisualBasic.Left式。

注意

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

適用於

另請參閱