PowerShell: How to Find Position of Character in String


You can use the following methods to find the position of a specific character in a string using PowerShell:

Method 1: Find Position of First Occurrence of Character in String

$my_string.IndexOf("Mavs")

This particular example returns the position of the first occurrence of “Mavs” in the string variable named $my_string.

Method 2: Find Position of First Occurrence After N Specific Characters in String

$my_string.IndexOf("Mavs", 1)

This particular example returns the position of the first occurrence of “Mavs” after character index 1 in the string variable named $my_string.

Method 3: Find Position of Last Occurrence of Character in String

$my_string.LastIndexOf("Mavs") 

This particular example returns the position of the last occurrence of “Mavs” in the string variable named $my_string.

The following examples show how to use each method in practice.

Example 1: Find Position of First Occurrence of Character in String

Suppose that we have the string “Mavs15Mavs19Mavs27” and that we would like to find the position of the first occurrence of “Mavs” in the string.

We can use the following syntax to do so:

$my_string = "Mavs15Mavs19Mavs27"
$my_string.IndexOf("Mavs")

The following screenshot shows how to use this syntax in practice:

PowerShell find position of character in string

Notice that this returns a value of 0 since this represents the position of the first occurrence of “Mavs” in the string:

  • Mavs15Mavs19Mavs27

Note: The index position of the first character in a string starts at 0.

Example 2: Find Position of First Occurrence After N Specific Characters in String

Suppose that we have the string “Mavs15Mavs19Mavs27” and that we would like to find the position of the first occurrence of “Mavs” in the string after character index 1.

We can use the following syntax to do so:

$my_string = "Mavs15Mavs19Mavs27"
$my_string.IndexOf("Mavs", 1)

The following screenshot shows how to use this syntax in practice:

PowerShell first position of character in string after n specific characters

Notice that this returns a value of 6 since this represents the position of the first occurrence of “Mavs” in the string:

  • Mavs15Mavs19Mavs27

Example 3: Find Position of Last Occurrence of Character in String

Suppose that we have the string “Mavs15Mavs19Mavs27” and that we would like to find the position of the last occurrence of “Mavs” in the string.

We can use the following syntax to do so:

$my_string = "Mavs15Mavs19Mavs27"
$my_string.LastIndexOf("Mavs")

The following screenshot shows how to use this syntax in practice:

PowerShell find position of last occurrence of character in string

Notice that this returns a value of 12 since this represents the position of the last occurrence of “Mavs” in the string:

  • Mavs15Mavs19Mavs27

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Text in String
PowerShell: How to Concatenate Strings

Leave a Reply