Manipulating strings
The .NET System.String
type offers a wide array of methods for manipulating or inspecting strings. The following methods are case-sensitive but are, in many cases, faster alternatives to using regular expressions, for situations when the time it takes for a script to run is important.
Working with data held in strings is an important part of any scripting language. The following sections explore selecting parts of a string, splitting, replacing, trimming, inserting, removing, and more.
Indexing into strings
In PowerShell, it is possible to access individual characters in a string by index (the zero-based position of a character) in the same way that you would access array elements using an index. Consider the following example:
$myString = 'abcdefghijklmnopqrstuvwxyz'
$myString[0] # This is a (the first character in the string)
$myString[-1] # This is z (the last character in the string)
Ranges of characters can be selected...