LTrim、RTrim 和 Trim 函数

返回一个 Variant (String) ,其中包含指定字符串的副本,其中没有前导空格 (LTrim) 、尾随空格 (RTrim) ,或者前导空格和尾随空格 (Trim) 。

语法

LTrim ( 字符串 )

RTrim ( 字符串 )

剪裁 ( 字符串 )

所需的 字符串参数 是任何有效的 字符串表达式 。 如果 字符串 包含 Null,则返回 Null

查询示例

Expression

结果

SELECT LTrim (ProductDesc) AS Expr1 FROM ProductSales;

返回字段“ProductDesc”中没有前导空格的字符串值,并显示在 Expr1 列中。

SELECT RTrim (ProductDesc) AS Expr1 FROM ProductSales;

返回字段“ProductDesc”中没有尾随空格的字符串值,并显示在 Expr1 列中。

SELECT LTrim (ProductDesc) AS NoTrailingSpace FROM ProductSales;

返回字段“ProductDesc”中没有前导空格的字符串值,并显示在“NoTrailingSpace”列中。

SELECT RTrim (ProductDesc) AS NoLeadingSpace FROM ProductSales;

返回字段“ProductDesc”中的字符串值,不带尾随空格,并显示在“NoLeadingSpaces”列中。

SELECT 剪裁 (ProductDesc) AS 剪裁 FROM ProductSales;

返回字段“ProductDesc”中的字符串值,不带前导空格和尾随空格,并显示在“剪裁”列中。

VBA 示例

注意: 下面的示例演示了如何在 Visual Basic for Applications (VBA) 模块中使用此函数。 有关使用 VBA 的详细信息,请在搜索旁边的下拉列表中选择“开发人员参考”,并在搜索框中输入一个或多个术语。

此示例使用 LTrim 函数对前导空格进行条带化, 使用 RTrim 函数从字符串变量中去除尾随空格。 它使用 Trim 函数来去除这两种类型的空间。

Dim MyString, TrimString
MyString = " <-Trim-> " ' Initialize string.
TrimString = LTrim(MyString)
' TrimString = "<-Trim-> ".
TrimString = RTrim(MyString)
' TrimString = " <-Trim->".
TrimString = LTrim(RTrim(MyString))
' TrimString = "<-Trim->".
' Using the Trim function alone
' achieves the same result.
TrimString = Trim(MyString)
' TrimString = "<-Trim->".

另请参阅

字符串函数以及如何使用它们

此信息是否有帮助?

为保护您的隐私,请不要在您的反馈意见中包含联系信息。 查看我们的 隐私策略