# FindOneOf - 脚本和图表功能

FindOneOf() 用于搜索字符串，以便从一组提供的字符中找到任意字符出现的位置。返回搜索集中任何字符的第 N 次出现的位置，其中 N 是函数的可选第三个参数。如果没有提供第三个参数，则返回第一个参数。如果未找到匹配值，则返回 0。

语法：

FindOneOf(text, char\_set[, count])

返回数据类型： 整数

参数

| 参数 | 描述 |
| --- | --- |
| text | 原始字符串。 |
| char\_set | 在 text 中搜索的字符集。 |
| count | 定义搜索哪一次出现的任何字符。例如，值为 2，则搜索第二次出现的。 |

示例：图表表达式

| 示例 | 结果 |
| --- | --- |
| `FindOneOf(  'my example text string', 'et%s' )` | 返回 `4`，因为 `e` 是示例字符串中的第四个字符。 |
| `FindOneOf(  'my example text string', 'et%s', 3 )` | 返回 `12`，因为搜索针对的是以下任何字符：`e`、`t`、`%` 或 `s`，而 `t` 在示例字符串 `12` 位置第三次出现。 |
| `FindOneOf(  'my example text string', '¤%&' )` | 返回 `0`，因为在示例字符串中找不到字符 `¤`、`%` 或 `&`。 |

## 示例 - FindOneOf 基本原理

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

#### 图表表达式

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 加载到名为 `Example` 的数据表中的数据集。

* 数据表中的一个字段名为 `Phrase`，其中包含要处理的原始文本字符串。

加载脚本

```
Example:
Load *
Inline
[Phrase
Many tiny beads
For a very long time
Has the potential for growth
];
```

结果

加载数据并打开工作表。创建新表并将该字段添加为维度：

* ` Phrase
                                   `

创建以下计算维度：

* `=FindOneOf(Phrase, 'e', 1)`

* `=FindOneOf(Phrase, 'y', 1)`

* `=FindOneOf(Phrase, 'ey', 1)`

* `=FindOneOf(Phrase, 'e', 2)`

* `=FindOneOf(Phrase, 'y', 2)`

* `=FindOneOf(Phrase, 'ey', 2)`

结果表

| Phrase | FindOneOf(Phrase, 'e', 1) | FindOneOf(Phrase, 'y', 1) | FindOneOf(Phrase, 'ey', 1) | FindOneOf(Phrase, 'e', 2) | FindOneOf(Phrase, 'y', 2) | FindOneOf(Phrase, 'ey', 2) |
| --- | --- | --- | --- | --- | --- | --- |
| For a very long time | 8 | 10 | 8 | 20 | 0 | 10 |
| Has the potential for growth | 7 | 0 | 7 | 12 | 0 | 12 |
| Many tiny beads | 12 | 4 | 4 | 0 | 9 | 9 |

对于每个计算的维度，输出返回每个短语的搜索集中字符的出现位置。例如，字母 `e` 的第一次出现分别是在每个短语的位置 `8`、`7` 和 `12`。每个短语中字母 `e` 的第二次出现是在位置 `20`、`12` 和 `0`（未找到）。相似地，字母 '`e` 或 `y`' 的第一次出现分别是在每个短语的位置 `8`、`7` 和 `4`。请注意，在短语 `For a very long time` 中，第一次出现 '`e`' 或 '`y`' 的位置是 `8` ，即 `e` ，第二次出现的位置是 `10` ，即`y` 。

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

#### 加载脚本

以下代码显示了如何在加载脚本中使用该函数。

```
Example:
Load *, FindOneOf(InputText, SearchFor, Occurrence) AS FindOneOf_Matches
Inline
InputText, SearchFor, Occurrence
my example text string, et%s,1
my example text string, et%s,3
my example text string, ¤%&,1
];
```

结果表

| InputText | SearchFor | Occurrence | FindOneOf\_Matches |
| --- | --- | --- | --- |
| 我的示例文本字符串 | et%s | 1 | 4 |
| 我的示例文本字符串 | et%s | 3 | 12 |
| 我的示例文本字符串 | ¤%& | 1 | 0 |

## 示例 - FindOneOf 场景

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

#### 图表表达式

概述

此示例使用 `FindOneOf` 函数分析客户订单数据集中的客户评论，并识别可能需要采取行动的订单。每个订单都有一个 `CustomerComment` 字段，客户可以在其中留言或评论他们的订单。通过分析评论，您可以识别特定的关键字或字符，如“！”、“@”、“#”， 这可能表明紧急情况或特殊要求。

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 加载到名为 `Example` 的数据表中的数据集。

* 数据表中有以下字段：

  * `TicketID` 票证的身份号码

  * `CustomerComment` 原始文本字符串

加载脚本

```
Example:
Load * inline [
TicketID, CustomerComment
1, I need this order ASAP!
2, Please confirm my order @12345.
3, Can you update my order?
4, I have a question about #discount.
5, Thank you!
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* ` TicketID
                                   `

* `CustomerComment`

创建以下计算维度：

* `=FindOneOf(CustomerComment, '!,@,#')`

* `=If(FindOneOf(CustomerComment, '!,@,#')>0,'High Priority','Normal Priority')`

结果表

| TicketID | 客户评论 | FindOneOf(CustomerComment, '!,@,#') | If(FindOneOf(CustomerComment, '!,@,#')&gt;0,'High Priority','Normal Priority') |
| --- | --- | --- | --- |
| 1 | 我需要尽快收到此订单！ | 23 | High Priority |
| 2 | 请确认我的订单 @12345。 | 25 | High Priority |
| 3 | 您能更新我的订单吗？ | 0 | 正常优先级 |
| 4 | 我有一个关于 #discount 的问题。 | 25 | High Priority |
| 5 | 谢谢！ | 10 | High Priority |

将 `FindOneOf` 函数的输出与脚本中加载的原始 `CustomerComment` 字符串值进行比较。使用 `!,@,#` 值，函数成功返回了这些字符在注释字符串中出现的位置。最后一项措施使用条件 `If` 语句来标识包含任何搜索字符的记录，并将其分配为 `High Priority`。如果没有找到任何搜索字符 `!,@,#`，例如第 3 行，则该票证被分类为 `Normal Priority`。