Converting VBScript's InStrRev Function
Definition: Returns the position of an occurrence of one string within another, from the end of string.
To the best of our knowledge, no Scripting Guy has ever used the InStrRev function. (Or, if they have, they won’t admit it.) What InStrRev does is examine a string value and then determine the last occurrence of a specified substring.
We don’t blame you. But here’s an example. Suppose you run InStrRev against the string value 1234x6789x1234, specifying the letter x as the substring you’re searching for. In that case InStrRev will return a 10, because the last x happens to be the 10th character in the string.
OK. Now, what if you want to perform this same trick in Windows PowerShell? No problem: just use the LastIndexOfAny method, like so:
$a = "1234x6789x1234" $b = $a.lastindexofany("x")
When you run those two commands and then echo back the value of $b you should get the following:
And, no, that’s not a mistake: in .NET the first character position in a string is actually position 0; that makes the 10th character (the character we're interested in) position 9. (A good thing to keep in mind if and when you start doing string manipulation in Windows PowerShell.)