LTrim、RTrim 和 Trim 函数

应用对象
Microsoft 365 专属 Access Access 2024 Access 2021 Access 2019 Access 2016

返回一个 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->".

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