$part function
$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.
Parameters
All parameters may contain symbolic references; these are resolved before the function is applied. See Examples below.
- string
- The original string.
- separator
- [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.
- index
- 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.
Examples
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
- <$part(<ProductVersion>,.,1)>
- Returns 5, the first subpart from the left.
- <$part(<ProductVersion>,.,1)>.<$part(<ProductVersion>,.,2)>
- Returns 5.1, the concatenation of the two leftmost version numbers.
- <$part(<ProductVersion>,.,-2)>
- Returns 3021, the second subpart from the right.
- <$part(<ProductVersion>,1,2)>
- Returns .302 because any character, including 1, can be used as a separator.
- <$part(<ProductVersion>,=,1)>
- 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.
- <$part(<SomeRandomString>,,2)>
- 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.
- <$part(<SomeRandomString>,,3)>
- Returns Another because any consecutive sequence of blanks is considered a single separator.
- <$part(<SomeRandomString>,`"`,3)>
- 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.