Strings.Left(String, Int32) 方法
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回字串,其中包含從字串的左邊開始的指定數目的字元。
public:
static System::String ^ Left(System::String ^ str, int Length);
C#
public static string Left (string? str, int Length);
C#
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
式。
VB
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
支援。
產品 | 版本 |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |