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 窗体 应用程序中使用,或者具有 Left 属性的任何其他类,则必须使用 Microsoft.VisualBasic.Left 完全限定函数。

注释

Visual Basic早期版本中的 LeftB 函数以字节而不是字符为单位返回字符串。 它主要用于转换双字节字符集 (DBCS) 应用程序中的字符串。 所有当前Visual Basic字符串都位于 Unicode 中,不再支持 LeftB

适用于

另请参阅