# Len - 指令碼與圖表函數

Len() 會傳回輸入字串的長度。

語法：

Len(text)

傳回的資料類型： 整數

引數

| 引數 | 描述 |
| --- | --- |
| text | 要評估的字串。 |

範例：圖表運算式

| 範例 | 結果 |
| --- | --- |
| `Len('Peter')` | 傳回 `5` |

## 範例 - Len 基礎事項

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/Len.htm)

#### 圖表運算式

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 載入到稱為 `Example` 之資料表格的資料集。

* 資料表格中稱為 `CustomerComment` 的一個欄位。該欄位包含要評估的原始文字字串。

載入指令碼

```
Example:
Load  * inline [
CustomerComment
Please deliver after 5 PM.
Thank you for the quick service!
Can you add a gift wrap?
];
```

結果

載入資料並開啟工作表。建立新的表格並將此欄位新增為維度：

* ` CustomerComment
                                   `

建立下列量值：

* `=Len(CustomerComment)`

結果表格

| CustomerComment | Len(CustomerComment) |
| --- | --- |
| Can you add a gift wrap? (可以新增禮品包裝嗎？) | 24 |
| Please deliver after 5 PM. (請於下午 5 點後配送。) | 26 |
| Thank you for the quick service! (感謝您的快速服務！) | 32 |

量值 `Len(CustomerComment)` 的輸出傳回 `CustomerComment` 輸入字串的長度。

## 範例 - Len 使用情境

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/Len.htm)

#### 圖表運算式

概述

此範例查看每個評論的長度，作為其深度或詳細程度的指標，藉此分析客戶意見回饋。

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 載入到稱為 `Example` 之資料表格的資料集。

* 資料表格中的欄位如下：

  * `TicketID`

  * `Description`

載入指令碼

```
Example:
Load * inline [
TicketID, Description
1001, "I received the wrong product."
1002, "The package arrived damaged, and I would like a replacement."
1003, "I've been trying to reset my password for two days, and I haven't received an email."
1004, "My order status shows 'Delivered' but I have not received my package yet."
1005, "Is product XYZ available in size Large?"
1006, "I need help with an exchange for a faulty product, and I've attached photos."
];
```

結果

載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度：

* ` TicketID
                                   `

* `Description`

建立下列計算維度：

* `=If(Len(Description) < 30, 'Short',If(Len(Description) <= 50, 'Medium', 'Long'))`，用來根據描述的長度計算並為工單指派類別 (短、中、長)。

結果表格

| TicketID | 描述 | If(Len(Description) &lt; 30, 'Short',If(Len(Description) &lt;= 50, 'Medium', 'Long')) |
| --- | --- | --- |
| 1001 | I received the wrong product. (我收到了錯誤的產品。) | 短 |
| 1002 | The package arrived damaged, and I would like a replacement. (包裹送達時已損壞，我想更換。) | 長 |
| 1003 | I've been trying to reset my password for two days, and I haven't received an email. (我已經嘗試重設密碼兩天了，但還沒有收到電子郵件。) | 長 |
| 1004 | Is product XYZ available in size Large? (產品 XYZ 有大尺寸嗎？) | 中 |
| 1005 | My order status shows 'Delivered' but I have not received my package yet. (我的訂單狀態顯示「已送達」，但我尚未收到包裹。) | 長 |
| 1006 | I need help with an exchange for a faulty product, and I've attached photos. (我需要協助更換有缺陷的產品，我已附上相片。) | 長 |

計算維度的輸出顯示如何使用 `Len` 函數，透過解譯文字字串的長度來分類資料。

## 範例 - 使用字串操縱的 Len 使用情境

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/Len.htm)

#### 圖表運算式

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 載入到稱為 `Example` 之資料表格的資料集。

* 資料表格中稱為 `InputText` 的一個欄位。

載入指令碼

```
Example:
Load  * inline [
InputText
this is a sample text string
capitalize first letter only
];
```

結果

載入資料並開啟工作表。建立新的表格並將此欄位新增為維度：

* ` InputText
                                   `

建立下列計算維度：

* 

  ` =Upper(Left(InputText,1))  `，用來將文字字串的第一個字母轉換為大寫。

* 

  `=Mid(InputText,Len(upper(Left(InputText,1)))+1)`，用來從文字字串移除第一個字母。

* 

  `=Upper(left(InputText,1)) & Mid(InputText,len(upper(left(InputText,1)))+1)`，用來結合第一個計算維度的輸出與第二個計算維度的輸出。

結果表格

| InputText | Upper(Left(InputText,1)) | Mid(InputText,Len(upper(Left(InputText,1)))+1) | Upper(left(InputText,1)) & mid(InputText,len(upper(left(InputText,1)))+1) |
| --- | --- | --- | --- |
| this is a sample text string | T | his is a sample text string | This is a sample text string |
| capitalize first letter only | C | apitalize first letter only | Capitalize first letter only |

在第一個計算維度中，結合了 ` Upper  ` 和 ` Left  `，以大寫傳回 `InputText` 的第一個字母。在第二個計算維度中，`Mid` 函數使用 `Len` 函數傳回從 `InputText` 移除第一個字元的文字字串。第三個計算維度結合了第一個和第二個計算維度，並傳回第一個字元為大寫的 `InputText` 字串。

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/Len.htm)

#### 載入指令碼

此範例使用與圖表運算式情境相同的函數 (Upper、Mid 和 Len)。載入指令碼建立新的欄位 NewInputText，這傳回第一個字元為大寫的 InputText。

```
Example:
Load InputText, First&Second as NewInputText;
Load *, mid(InputText,len(First)+1) as Second;
Load *, upper(left(InputText,1)) as First;
Load  * inline [
InputText
this is a sample text string
capitalize first letter only ];
```

結果表格

| InputText | NewInputText |
| --- | --- |
| this is a sample text string | This is a sample text string |
| capitalize first letter only | Capitalize first letter only |