LTrim、RTrim 與 Trim 函數

套用到
Microsoft 365 Access Access 2024 Access 2021 Access 2019 Access 2016

傳回包含指定字串複本 ( (字串字串) 變體,且不包含前置空格 LTrim) 、結尾空格 (RTrim) ,或同時包含前置和結尾空格 (Trim) 。

語法

LTrim (字串)

RTrim (字串)

修剪 (字串)

必要的字串引數是任何有效的字串運算式。 如果 字串 包含 Null,則會傳回 Null

查詢範例

運算式 結果
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” 中的字串值,但不含前置和結尾空格,並顯示在 “Trimed” 資料行中。

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

字串函數及其使用方法