IndexOf is Case Sensitive

Whilst doing some string manipulation recently was having issues getting the correct data, or rather the data I expected. Consider the following:

PS ...Scripts>$SQLCmd = "SELECT Name, Vendor From esx_config WHERE Name='myesx1'"
PS ...Scripts>$Position = $SQLCmd.IndexOf(" FROM ")
PS ...Scripts>$Position
-1

The result is a -1, which is not correct, however does make sense when looking for correct case.

After some hunting around and numerous search result was able to sort my issue and made the following adjustment.

PS ...Scripts>$SQLCmd = "SELECT Name, Vendor FrOm esx_config WHERE Name='myesx1'"
PS ...Scripts>$Position = $SQLCmd.IndexOf(" FROM ", [StringComparison]"CurrentCultureIgnoreCase")
PS ...Scripts>$Position
19

Now I get the correct position.

Only tested on Powershell v2.0.

Note: The overload does not work on an Array. 🙁

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.