# Power Automate: Find Text Position With indexOf()

> For the full documentation index, see https://250highstreet.com/llms.txt

> Find where a word or character sits inside a text value in Power Automate. Covers indexOf, lastIndexOf, nthIndexOf, the -1 result and case rules.

Use the indexOf() expression. indexOf('Invoice 2026-1042', '2026') returns 8, the zero-based position where the search text starts. If the text is not found, it returns -1.

## What does indexOf() return?

indexOf(text, searchText) returns the position where searchText first appears in text. Positions start at 0, so the first character is at position 0.

Add a Compose action and enter the expression on the Expression tab. For example, indexOf('jane@contoso.com', '@') returns 4.

## Is indexOf() case sensitive?

No. Microsoft documents indexOf() as case-insensitive. The expression indexOf('Invoice', 'VOICE') returns 2.

If case matters, test the match with contains(), which Microsoft documents as case-sensitive.

## What does -1 mean, and how do you handle it?

A result of -1 means the search text is not in the value. If you pass -1 into substring(), the flow fails, so test for it first. In the domain example below, a missing @ does not stop the flow: add(-1, 1) is 0, so substring() returns the whole value. The Condition prevents that.

Use a Condition action with the expression greater(indexOf(variables('Email'), '@'), -1). Continue to your extract step only on the Yes branch.

## How do you find the last match instead of the first?

Use lastIndexOf(). It works like indexOf() but returns the position of the last match. For a file name such as report.final.docx, lastIndexOf('report.final.docx', '.') returns 12, the dot before the extension.

To find the second or third match, use nthIndexOf(text, searchText, occurrence). For example, nthIndexOf('a-b-c-d', '-', 2) returns 3.

## How do you extract text with indexOf()?

Combine indexOf() with substring(). To get the domain from an email address, find the @ sign and take everything after it:

substring(variables('Email'), add(indexOf(variables('Email'), '@'), 1))

The add(..., 1) skips the @ itself. For the full rules on start index and length, read [substring: extract text](/guides/power-automate-substring-extract-text/).

## Should you use indexOf() or contains()?

If you only need to know whether the text is there, use contains(). It returns true or false, and you do not need to check for -1. See [the contains function](/guides/power-automate-contains-function-text-array/).

Use indexOf() when you need the position, for example to cut the text at that point. To break a value at a fixed separator, [split](/guides/power-automate-split-string/) is often shorter.

## Why does indexOf() fail on an empty or null value?

indexOf() expects text. If the column is empty and the dynamic content returns null, the flow can stop with a type error. Wrap the value in coalesce(value, '') to supply an empty text value.

The guide on [invalid type, expected string but got null](/guides/power-automate-invalid-type-expected-string-but-got-null/) shows the fix in full.

## FAQ

### Does indexOf() start counting at 0 or 1?

It starts at 0. The first character of the text is position 0.

### Can indexOf() search an array?

No. It searches text only. To check whether an array holds a value, use contains().

### What is the difference between indexOf() and nthIndexOf()?

indexOf() returns the first match. nthIndexOf() returns the match number you ask for, such as the second or third.

## Sources

- [Reference guide to workflow expression functions in Azure Logic Apps and Power Automate](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference)

## Related guides

- [Power Automate substring(): Get Part of a Text Value](/guides/power-automate-substring-extract-text/)
- [Power Automate contains(): Check Text or a List for a Value](/guides/power-automate-contains-function-text-array/)
- [Split text into parts in Power Automate](/guides/power-automate-split-string/)
- [Fix "Invalid type. Expected String but got Null" in Power Automate](/guides/power-automate-invalid-type-expected-string-but-got-null/)

Source: https://250highstreet.com/guides/power-automate-indexof-find-position-in-text/
