'How do I find the position of substring in PowerShell after position x?

Is there a simple way to do so; I can't find any. Am I obliged to scan character by character?

I don't want a string as return, but a position so do not suggest SubString.



Solution 1:[1]

Yes, you can. The IndexOf() method has an overload that takes a startIndex argument:

PS C:\> "WordsWordsWords".IndexOf("Words")
0
PS C:\> "WordsWordsWords".IndexOf("Words", 2)
5

In the second example, we look for the index of the substring "Words" starting after character index 2.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen