Using IndexOf()
Method
Use the IndexOf()
to find the position of character in string in PowerShell. indexOf()
method returns index of first occurrence of character in String.
1 2 3 4 5 6 |
$string = "Hello, world!" $position = $string.IndexOf(",") Write-Host "Input string: $string" Write-Host "Position of comma: $position" |
1 2 3 4 |
Input string: Hello, world! Position of comma: 5 |
In the above code, the output indicates that the input string is Hello, world!
and the position of the comma character
in the string is 5
.
The IndexOf()
method returns the zero-based index of the specified character’s first occurrence or substring in the string. In this case, the comma character is the first occurrence of the specified character, so the method returns 5.
Using the LastIndexOf()
Method
Use the LastIndexOf()
method to find the character’s position in a string.
1 2 3 4 5 6 7 8 |
$string = "Hello, world, again!" $lastCommaPosition = $string.LastIndexOf(",") $lastWorldPosition = $string.LastIndexOf("world") Write-Host "Input string: $string" Write-Host "Position of last comma: $lastCommaPosition" Write-Host "Position of last 'world': $lastWorldPosition" |
1 2 3 4 5 |
Input string: Hello, world, again! Position of last comma: 12 Position of last 'world': 7 |
The LastIndexOf()
method is used to find the position of the last occurrence of a character and a substring in a string. This code section is similar to the previous one, but it uses the LastIndexOf()
method to find an index of the last occurrence of the substring world
in the $string
variable and assigns the result to a variable called $lastWorldPosition
.
Further reading:
Using Regular Expression
Use the regular expression method to find the position of a character in a string.
1 2 3 4 5 6 7 8 9 |
$string = "Hello, world!" $pattern = "," $match = $string | Select-String -Pattern $pattern $position = $match.Matches.Index Write-Host "Input string: $string" Write-Host "Search pattern: $pattern" Write-Host "Match position: $position" |
1 2 3 4 5 |
Input string: Hello, world! Search pattern: , Match position: 5 |
Regular expressions are patterns that are used to match and manipulate text. In this case, we can use a regular expression pattern to match all spaces in the string & replace them with an empty string. The above code defines two variables, $string
and $pattern
, representing the input string and the character we want to locate, respectively.
The Select-String
cmdlet is used to search the input string for the pattern. This cmdlet takes the input string as a pipeline input and searches for the pattern specified by the -Pattern
flag. In this example, the pattern is simply a comma. The Select-String
cmdlet’s output is a MatchInfo
object, which includes information about the match.
We can obtain the position of the match by accessing the Index
property of the Matches
property of the MatchInfo
object. This value represents the index of the first character in the input string that matches the specified pattern.
That’s all about how to find position of character in String in PowerShell.