# Len 脚本和图表功能

Len() 用于返回输入字符串的长度。

语法：

Len(text)

返回数据类型： 整数

参数

| 参数 | 描述 |
| --- | --- |
| text | 要计算的字符串。 |

示例 - 图表表达式

| 示例 | 结果 |
| --- | --- |
| `Len('Peter')` | 返回 `5` |

## 示例 - Len 基本原理

[](https://help.qlik.com/en-US/cloud-services/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. | 26 |
| Thank you for the quick service! | 32 |

度量 `Len(CustomerComment)` 的输出返回 `CustomerComment` 输入字符串的长度。

## 示例 - Len 场景

[](https://help.qlik.com/en-US/cloud-services/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. | Short |
| 1002 | The package arrived damaged, and I would like a replacement. | Long |
| 1003 | I've been trying to reset my password for two days, and I haven't received an email. | Long |
| 1004 | Is product XYZ available in size Large? | 中等 |
| 1005 | My order status shows 'Delivered' but I have not received my package yet. | Long |
| 1006 | I need help with an exchange for a faulty product, and I've attached photos. | Long |

计算维度的输出显示了如何使用 `Len` 函数通过解释文本字符串的长度对数据进行分类。

## 示例 - 使用字符串操作的 Len 场景

[](https://help.qlik.com/en-US/cloud-services/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/cloud-services/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 |