# 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`」というデータ テーブルにロードされるデータセット。

* データ テーブル内の 1 つの項目は `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'))`、説明文の長さに基づいて、チケットのカテゴリ (Short (短)、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? | Medium |
| 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`」というデータ テーブルにロードされるデータセット。

* データ テーブル内の 1 つの項目は `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)`、最初の計算軸からの出力と 2 番目の計算軸からの出力を組み合わせます。

結果テーブル

| 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` の最初の文字を大文字として返します。2 番目の計算軸では、`Mid` 関数は、`Len` 関数を使用して、`InputText` から最初の文字を取り除いたテキスト文字列を返します。3 番目の計算軸は、1 番目と 2 番目の計算軸を結合し、最初の文字が大文字の `InputText` 文字列を返します。

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

#### ロード スクリプト

この例では、チャートの数式シナリオと同じ関数 (Upper、Mid、Len) を使用します。ロード スクリプトは、先頭文字が大文字の InputText を返す新しい項目 NewInputText を作成します。

```
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 |