# SubStringCount スクリプトおよびチャート関数

SubStringCount()は、指定された文字列テキストに、指定されたサブストリングが出現する回数を返します。出現しない場合は、0 を返します。

構文:

SubStringCount(text, sub\_string)

戻り値データ型： 整数

引数

| 引数 | 説明 |
| --- | --- |
| text | 元の文字列。 |
| sub\_string | text入力文字列に 1 回以上現れる文字列。 |

例: チャートの数式

| 例 | 結果 |
| --- | --- |
| `SubStringCount( 'abcdefgcdxyz', 'cd' )` | `2` を返します |
| `SubStringCount( 'abcdefgcdxyz', 'dc' )` | `0` を返します |

## 例 - SubStringCount の基本

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

#### チャートの数式

概要

データ ロード エディターを開き、以下のロード スクリプトを新しいセクションに追加します。

ロード スクリプトには次が含まれています:

* 「`Example`」というデータ テーブルにロードされるデータセット。

* データ テーブル内の 1 つの項目は `FruitSentence` と呼ばれます。

ロード スクリプト

```
Example:
Load * inline [
FruitSentence
"I love eating an apple every day.Apple juice is great too!"
"Apple pie is my favorite dessert. I also enjoy apple jam."
"There is an apple and a banana on the table."
"I don't like apples as much as oranges."
"An apple a day keeps the doctor away, but two apples are even better!"
];
```

結果

データをロードしてシートを開きます。新しいテーブルを作成し、この項目を軸として追加します:

* ` FruitSentence
                                   `

次の計算軸を作成します。

* `=SubStringCount(FruitSentence, 'apple')`、`FruitSentence` に `apple` という単語が何回出てくるかを計算します。

Results table

| FruitSentence | SubStringCount(FruitSentence, 'apple') |
| --- | --- |
| An apple a day keeps the doctor away, but two apples are even better! | 2 |
| Apple pie is my favorite dessert. I also enjoy apple jam. | 1 |
| I don't like apples as much as oranges. | 1 |
| I love eating an apple every day. Apple juice is great too! | 1 |
| There is an apple and a banana on the table. | 1 |

テーブルは、`FruitSentence` の軸で昇順にソートされます。`SubStringCount` 関数は、各文中に `apple` という単語が出現する回数を返します。たとえば、最初の文では、この単語が文中に 2 回出現するため、`2` が返されます。2 番目と 4 番目の文では、検索語 `apple` の大文字と小文字が一致しないため、関数は単語 `Apple` を無視します。

ヒント メモ大文字小文字を区別しない検索を行うには、まず入力文字列を大文字または小文字に変換し、その結果に対して `SubStringCount` 関数を使用します。例えば、大文字と小文字に関係なく、apple という単語が出現するすべての場所を検索するには、次のメジャー式を使用します: `=SubStringCount(Upper(FruitSentence),'APPLE')`。この式では、`SubStringCount` 関数と`Upper` 関数を使用して文字列全体を大文字に変換してから、`APPLE` という単語を検索します。

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

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

次のコードは、ロード スクリプトで関数を使用する方法を示しています。 この例では、`SubStringCount` 関数と `Upper` 関数を使って文字列全体を大文字に変換し、`APPLE` という単語を検索する先行ロードが含まれています。

```
Example:
Load *,
SubStringCount(Upper(FruitSentence),'APPLE') as SubStringCount_APPLE;
Load * inline [
FruitSentence
"I love eating an apple every day. Apple juice is great too!"
"Apple pie is my favorite dessert. I also enjoy apple jam."
"There is an apple and a banana on the table."
"I don't like apples as much as oranges."
"An apple a day keeps the doctor away, but two apples are even better!"
];
```

結果テーブル

| FruitSentence | SubStringCount\_APPLE |
| --- | --- |
| I love eating an apple every day. Apple juice is great too! | 2 |
| Apple pie is my favorite dessert. I also enjoy apple jam. | 2 |
| There is an apple and a banana on the table. | 1 |
| I don't like apples as much as oranges. | 1 |
| An apple a day keeps the doctor away, but two apples are even better! | 2 |

## 例 - SubStringCount のシナリオ

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

#### チャートの数式

概要

果物製品のデータセットには、製品の説明を示す項目があります。この例では、apple、banana、orange という果物の名前がそれぞれの説明文に登場する回数を計算します。

データ ロード エディターを開き、以下のロード スクリプトを新しいセクションに追加します。

ロード スクリプトには次が含まれています:

* 「`Example`」というデータ テーブルにロードされるデータセット。

* データ テーブル内の 1 つの項目は `ProductDescription` と呼ばれます。

ロード スクリプト

```
Example:
Load * inline [
ProductDescription
"Fresh apple and banana smoothie."
"Organic apple, banana, and orange juice. Great for breakfast."
"A refreshing orange soda made with real orange juice."
"Banana chips with apple and cinnamon flavor."
"Delicious apple pie with a hint of cinnamon and vanilla."
"Tropical banana and orange mix for smoothies."
];
```

結果

データをロードしてシートを開きます。新しいテーブルを作成し、この項目を軸として追加します:

* `ProductDescription`

次の計算軸を作成します。

* `=SubStringCount(ProductDescription, 'apple')`、文字列 `ProductDescription` 中の単語 `apple` の出現回数を計算します。

* `=SubStringCount(ProductDescription, 'banana')`、文字列 `ProductDescription` 中の単語 `banana` の出現回数を計算します。

* `=SubStringCount(ProductDescription, 'orange')`、文字列 `ProductDescription` 中の単語 `orange` の出現回数を計算します。

Results table

| ProductDescription | SubStringCount(ProductDescription, 'apple') | SubStringCount(ProductDescription, 'banana') | SubStringCount(ProductDescription, 'orange') |
| --- | --- | --- | --- |
| A refreshing orange soda made with real orange juice. | 0 | 0 | 2 |
| Banana chips with apple and cinnamon flavor. | 1 | 0 | 0 |
| Delicious apple pie with a hint of cinnamon and vanilla. | 1 | 0 | 0 |
| Fresh apple and banana smoothie. | 1 | 1 | 0 |
| Organic apple, banana, and orange juice. Great for breakfast. | 1 | 1 | 1 |
| Tropical banana and orange mix for smoothies. | 0 | 1 | 1 |

`SubStringCount` 関数の出力は、各サブストリング (`apple, banana, orange`) が製品説明文中に出現するインスタンス数を正常に返します。たとえば、最初の説明では、`orange` という単語のインスタンスが 2 つあるため、関数は 2 を返します。引数 `sub_string` は大文字と小文字を区別するため、2 番目の説明では、`Banana` という単語はメジャー式の `banana` という部分文字列と一致しないため、この関数はカウントしません。

ヒント メモ大文字小文字を区別しない検索を行うには、まず入力文字列を大文字または小文字に変換し、その結果に対して `SubStringCount` 関数を使用します。例えば、大文字と小文字に関係なく、apple という単語が出現するすべての場所を検索するには、次のメジャー式を使用します: `=SubStringCount(Upper(ProductDescription),'APPLE')`。この式では、`SubStringCount` 関数と`Upper` 関数を使用して文字列全体を大文字に変換してから、`APPLE` という単語を検索します。

## 例 - ネストされたデータを解析する SubStringCount のシナリオ

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

#### チャートの数式

概要

IT レコードのデータセットには、変更 ID 番号とカテゴリのリストが含まれています。データ テーブルでは、Categories 項目は、最上位の親カテゴリとそれに続くすべてのサブ カテゴリをリストする複数レベルの階層構造になっています (例: Software &gt; Program &gt; Patch)。このシナリオでは、SubStringCount 関数と SubField 関数の組み合わせを使用して、この不規則なカテゴリ階層から最後の値、つまりリーフ値を選択する方法を示します。

データ ロード エディターを開き、以下のロード スクリプトを新しいセクションに追加します。

ロード スクリプトには次が含まれています:

* 「Example」というデータ テーブルにロードされるデータセット。

* データ テーブル内の次の項目:

  * ChangeID には、処理される元の変更 ID が含まれています。

  * Categories には変更カテゴリのリストが含まれています。カテゴリはセミコロンで区切られ、最上位の親カテゴリからもっとも詳細なサブカテゴリまで、左から右へ階層順にリストされます。

ロード スクリプト

```
Example:
Load * inline [
ChangeID,Categories
CHG00100,Software;Program;Patch
CHG00101,Hardware;Server
CHG00102,Hardware;Network;Router
];
```

結果

データをロードしてシートを開きます。新しいテーブルを作成し、これらの項目を軸として追加します:

* ChangeID

* Categories

次の計算軸を作成します。

* =SubStringCount(Categories,';')+1、変更階層内のカテゴリ レベルの数を計算します。

* =SubField(Categories, ';' ,SubStringCount(Categories,';')+1)、変更階層の一番右のカテゴリを抽出します。

Results table

| ChangeID | Categories | ​SubStringCount(Categories,';')+1 | SubField(Categories, ';' ,SubStringCount(Categories,';')+1) |
| --- | --- | --- | --- |
| CHG00100 | Software;Program;Patch | 3 | Patch |
| CHG00101 | Hardware;Server | 2 | Server |
| CHG00102 | Hardware;Network;Router | 3 | Router |

この例では、SubStringCount 関数は SubField 関数内にネストされています。最初の計算軸、SubStringCount() は、Categories 項目のネストされた値の数を計算します。これはセミコロン (';') の数を数え、その結果に 1 を足すことで決定されます。この出力は、SubField 関数の 3 番目のパラメーターとして挿入され、Categories 項目にネストされた右端のカテゴリが抽出されます。

例えば、ChangeID エントリー CHG00100 の結果を見てみましょう。計算された最初の軸では、SubStringCount は 2 です。式はこの結果に 1 を追加して、Categories 項目には 3 つのネストされたカテゴリー値、Software、Program、および Patch があることを判断します。次に、SubField 関数は、計算された 2 つ目の軸で、この結果を使用して 3 つ目のカテゴリー、Patch を抽出します。