Text.PositionOfAny
TextReturns the position of the first character in a text value that matches any character in a given list.
Syntax
Text.PositionOfAny(text as text, characters as list, optional occurrence as nullable number) as anyParameters
| Name | Type | Required | Description |
|---|---|---|---|
text | text | Yes | The text value to search within. |
characters | list | Yes | A list of single-character text values to search for. |
occurrence | number | No | Specifies which occurrence to find. Use Occurrence.First (default), Occurrence.Last, or Occurrence.All. |
Return Value
any — The zero-based position of the first matching character, -1 if none found, or a list of positions when Occurrence.All is used.
Remarks
Text.PositionOfAny searches text for any character that appears in the characters list and returns the position of the first such match. Returns -1 if no character from the list is found — not an error. Like Text.PositionOf, always check for -1 before using the result as an offset.
The characters parameter is a list of single-character text values. Each element is matched independently as a single character, not as a substring. This is the key difference from Text.PositionOf, which searches for a multi-character substring. Use Text.PositionOfAny when you want to find the first occurrence of any one of a set of characters — for example, the first digit, the first punctuation character, or the first whitespace.
The optional occurrence parameter accepts:
- Occurrence.First (default) — position of the first matching character
- Occurrence.Last — position of the last matching character
- Occurrence.All — a list of all positions where any matching character appears
Note that Text.PositionOfAny does not accept a comparer parameter — the character matching is always case-sensitive. To perform case-insensitive matching, normalize the text with Text.Lower or Text.Upper before calling this function, and also lowercase or uppercase your characters list accordingly.
Examples
Example 1: Find the position of the first digit in a string
Text.PositionOfAny("E001", {"0","1","2","3","4","5","6","7","8","9"})Result | |
|---|---|
| 1 | 1 |
Example 2: Find the first separator character in a phone number
Text.PositionOfAny("(555) 123-4567", {"(",")"," ","-"})Result | |
|---|---|
| 1 | 0 |
Example 3: Get all positions of punctuation in a string
Text.PositionOfAny("alice.smith@example.com", {".", "@"}, Occurrence.All)Result | |
|---|---|
| 1 | 5,11,19 |
Example 4: No match returns -1
Text.PositionOfAny("AliceSmith", {"0","1","2","3","4","5","6","7","8","9"})Result | |
|---|---|
| 1 | -1 |