LTrim、RTrim 和 Trim 函数

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

返回包含指定字符串副本的字符串 (变体) ,其中不带前导空格 (LTrim) 、尾随空格 (RTrim) ,或同时包含前导和尾随空格 (剪裁) 。

语法

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 Trim (ProductDesc) AS Trimmed FROM ProductSales; 返回字段“ProductDesc”中的字符串值,不带前导和尾随空格,显示在列“Trimmed”中。

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->".

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