$part(string, separator, index)
The $part function returns the index-th subpart of string, where subparts are separated by the separator character. If string is empty or contains fewer subparts than the requested index, or if index is 0 (zero) or non-numerical, then an empty string is returned. The original string is not modified.
As a special case, if separator is empty or only contains spaces, then the parts of string are assumed to be separated by one or more blanks (spaces, tabs, newline). In that case (only), the $part function recognizes "quoted" subparts and does not treat blanks inside the quotes as separators. The surrounding quotes are stripped from the subpart before it is returned.
In all cases except "quoted" subparts in a blank-separated string, leading and trailing spaces are stripped from the subpart before it is returned.
All parameters may contain symbolic references; these are resolved before the function is applied. See Examples below.
- The original string.
- [Optional] The character that is used to separate subparts in the string. If this parameter is empty or consists only of blanks, then the parts of string are assumed to be separated by sequences of blanks (spaces, tabs, newlines). Tip: To use a comma as a separator, enclose it in backticks like this: `,` to avoid interpretation of the comma as an argument separator of the $part function itself.
- The 1-based index of the part to return. The first (leftmost) part is 1, the next 2, and so on. If index is negative, parts are counted from the right: -1 is the rightmost part, -2 the one to the left of that, and so on.
Here are some usage examples for this function. They assume the following variables:
- <ProductVersion> contains 5.1.3021.4
- <SomeRandomString> contains Part1 "Part the next" Another Part
- Returns 5, the first subpart from the left.
- Returns 5.1, the concatenation of the two leftmost version numbers.
- Returns 3021, the second subpart from the right.
- Returns .302 because any character, including 1, can be used as a separator.
- Returns 5.1.3021.4 because the '=' separator does not appear in the string, which is therefore assumed to consist of one part only: the entire string.
- Returns Part the next because the missing separator character causes $part to use sequences of blanks as separators, and also to treat "quoted" subparts as a single unit. Note that the quotes themselves have been stripped from the returned substring.
- Returns Another because any consecutive sequence of blanks is considered a single separator.
- Returns Another Part with leading spaces removed. In this case, the " quote character is used as the separator character and does not get special treatment. Note the use of `backticks` to ensure that the " character is passed uninterpreted to the $part function.