# Repeat 指令碼與圖表函數

Repeat() 會形成一個字串，包含的輸入字串重複第二個引數所定義的次數。

語法：

Repeat(text[, repeat\_count])

傳回的資料類型： 字串

引數

| 引數 | 描述 |
| --- | --- |
| text | 原始字串。 |
| repeat\_count | 定義字串 text 中要在輸出字串中重複的字元的次數。 |

範例：圖表運算式

| 範例 | 結果 |
| --- | --- |
| `Repeat( ' * ', 4 )` | 傳回 `****` |

## 範例 - Repeat 基礎事項

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

#### 圖表運算式

概述

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

載入指令碼包含：

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

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

載入指令碼

```
Example:
Load * inline [
InputText
hello world!
hOw aRe you?
];
```

結果

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

* ` InputText
                                       `

建立下列計算維度：

* `=Repeat(InputText,2)`，用來計算 `InputText` 中值的重複次數。在此範例中，每個字串將重複兩次。

結果表格

| InputText | Repeat(InputText,2) |
| --- | --- |
| 哈囉世界！ | `hello world!hello world!` |
| 你好嗎？ | `hOw aRe you?hOw aRe you?` |

`Repeat` 函數的輸出包括 `InputText` 字串的兩個執行個體，例如，第 1 列傳回 `hello world!hello world!`。

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

#### 載入指令碼

以下程式碼顯示如何在載入指令碼中使用該函數。

```
Example:
Load *,
repeat(String,2) as Repeat;
Load * inline [
String
hello world!
hOw aRe you? ];
```

結果表格

| 字串 | 重複 |
| --- | --- |
| hello world! | hello world!hello world! |
| hOw aRe you? | hOw aRe you?hOw aRe you? |

## 範例 - Repeat 使用情境

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

#### 圖表運算式

概述

此範例使用客戶滿意度資料，將數字滿意度分數轉換為使用表情符號 ☺ 的視覺呈現方式。

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

載入指令碼包含：

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

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

  * `Customer`

  * `SatisfactionScore`

載入指令碼

```
Example:
Load * inline [
Customer, SatisfactionScore
John,4
Mary,2
Peter,5
Susan,3
];
```

結果

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

* ` Customer
                                       `

* `SatisfactionScore`

建立下列計算維度：

* `=Repeat('☺',(SatisfactionScore))`，用來透過表情符號在視覺上計算和顯示分數。

結果表格

| 客戶 | SatisfactionScore | Repeat('☺',SatisfactionScore) |
| --- | --- | --- |
| John | 4 | ☺☺☺☺ |
| 瑪麗 | 2 | ☺☺ |
| 彼得 | 5 | ☺☺☺☺☺ |
| 蘇珊 | 3 | ☺☺☺ |

`Repeat` 函數的輸出顯示客戶滿意度的視覺呈現，使用笑臉表情符號來表示數字滿意度分數。

## 範例 - Repeat 進階使用情境

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

#### 圖表運算式

概述

客戶帳戶資料集包含具有不同字元長度的帳號。此範例示範如何使用 Repeat 函數標準化帳號長度。

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

載入指令碼包含：

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

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

載入指令碼

```
Example:
Load * inline [
AccountNo
123
45125783
41523
489654
];
```

結果

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

* AccountNo

建立下列計算維度：

* =Repeat(0,10 - Len(AccountNo)) & AccountNo，用來透過零填補帳號，使其總長度為 10 個字元。

結果表格

| AccountNo | Repeat(0, 10 - Len(AccountNo)) & AccountNo |
| --- | --- |
| 123 | 0000000123 |
| 41523 | 0000041523 |
| 489654 | 0000489654 |
| 45125783 | 0045125783 |

Repeat 函數的輸出顯示所有帳號的標準化長度為 10 個字元。