# SubStringCount 指令碼與圖表函數

SubStringCount() 會傳回輸入字串文字中指定子字串的出現次數。如果沒有任何相符項，則會傳回 0。

語法：

SubStringCount(text, sub\_string)

傳回的資料類型： 整數

引數

| 引數 | 描述 |
| --- | --- |
| text | 原始字串。 |
| sub\_string | 輸入字串 text 內可能出現一次或多次的字串。 |

範例：圖表運算式

| 範例 | 結果 |
| --- | --- |
| `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` 之資料表格的資料集。

* 資料表格中稱為 `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')`，用來計算字詞 `apple` 在 `FruitSentence` 出現了幾次。

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`，因為該字詞在句子中出現了兩次。在第二句和第四句中，函數忽略字詞 `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` 之資料表格的資料集。

* 資料表格中稱為 `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')`，用來計算字詞 `apple` 在字串 `ProductDescription` 出現的次數。

* `=SubStringCount(ProductDescription, 'banana')`，用來計算字詞 `banana` 在字串 `ProductDescription` 出現的次數。

* `=SubStringCount(ProductDescription, 'orange')`，用來計算字詞 `orange` 在字串 `ProductDescription` 出現的次數。

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`) 在產品描述中出現的次數。例如，在第一個描述中，函數傳回 2，因為字詞 `orange` 有兩個執行個體。`sub_string` 引數區分大小寫，因此在第二個描述中，函數不會計入字詞 `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 欄位源自多層階層結構，其中列出了頂層父類別，後面跟著所有子類別，例如軟體 &gt; 程式 &gt; 修補。此情境示範如何使用 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 函數的第三個參數插入，以擷取內嵌於 Categories 欄位中最右側的類別。

例如，檢閱 ChangeID 項目 CHG00100 的結果。在第一個計算維度中，SubStringCount 為 2。然後運算式會對該結果加 1，以確定 Categories 欄位有三個巢狀類別值：Software、Program 和 Patch。然後，在第二個計算維度中，SubField 函數會使用此結果擷取第三個類別 Patch。