A split operation operator for a string is provided in PowerShell. The-split operator splits a string into multiple strings.
Split operator
The split operator is used to split one or more strings into multiple substrings. You can change the following elements of a split operation:
(1) delimiter. The default is blank, but you can specify a character, a string, a pattern, or a script block to set the delimiter.
(2) The maximum number of substrings. The default setting is to return all substrings. If the specified number is less than the number of substrings, the remaining substrings are merged into the last substring.
(3) Options for specifying the delimiter matching criteria, such as SimpleMatch and Multiline.
The syntax for the split operator is as follows (3 syntax, meaning there are three ways to use it):
-split <String><String>-split <delimiter>[,<max-substrings>[, "<Options>"]]< string>-split {<scriptblock>} [, <max-substrings>]
Note that there is no name for the parameter in the operator, and only the value of the parameter is included. Therefore, the parameter values must appear in the order specified in the syntax. The-split operation has a case-sensitive format (the default is case-insensitive), and the-isplit operator is case-insensitive. The-csplit operator is case-sensitive, which means that case is considered when applying a delimiter rule.
An example of the parameters of the-split operator is shown below.
<String>
Specifies one or more strings to split. If it is more than one string, all strings are split using the same delimiter given. For example:
PS d:\>-split "abc def GHI" Abcdefghi
<Delimiter>
A delimiter that identifies the character where the substring ends (that is, what divides a string into multiple substrings). The default delimiter is blank, including whitespace and non-printable characters such as newline (' N) and tabs (' t). When a string is split, delimiters are omitted from all substrings. For example:
PS d:\> "Ab:cd:ef"-split ":" ABCdef
<Max-substrings>
An optional parameter that specifies the maximum number of substrings that are returned. The default setting is to split all substrings through the delimiter. if the number of substrings is greater than the maximum number, the extra substrings are merged into the last substring. If the number of substrings is less than the maximum number, all substrings are returned. all substrings are returned when a value of 0 or negative is used.
If you submit multiple strings (a set of strings) to the split operator, the max-substrings limit is applied to each string individually. For example:
PS d:\> $a = "Monday,tuesday,wesday,thurday,friday,saturday,sunday" PS d:\> $a-split ",", 4mondaytuesdaywesdaythurday,friday,saturday,sunday
It is important to note that the parameters are separated by commas.
<Options>
This parameter is only valid if the <Max-substrings> parameter is used in the statement, and you need to enclose the option name in quotation marks.
The syntax for the Options parameter is:
"SimpleMatch [, IgnoreCase]" "[Regexmatch] [, IgnoreCase] [, cultureinvariant][,ignorepatternwhitespace][, Explicitcapture][,singleline |, Multiline] "
the SimpleMatch options are :
(1) SimpleMatch: Use simple string comparisons when calculating delimiters. Not to be used with Regexmatch.
(2) IgnoreCase: Enforces a case-insensitive match, even if the-csplit operator is specified.
the Regexmatch options are :
(1) Regexmatch: Use regular expression matching to calculate delimiters. This is the default behavior. Not to be used with SimpleMatch.
(2) IgnoreCase: Enforces a case-insensitive match, even if the-csplit operator is specified.
(3) Cultureinvariant: Ignores culture differences in language when calculating delimiters. Valid only when used with Regexmatch.
(4) Ignorepatternwhitespace: Ignores non-escaped whitespace and notes marked with pound sign (#). Valid only when used with Regexmatch.
(5) Multiline: Use Multiline mode to recognize the beginning and end of lines and strings. Valid only when used with Regexmatch. The default setting is Singleline.
(6) Singleline: Use Singleline mode to recognize only the beginning and end of a string. Valid only when used with Regexmatch. The default setting is Singleline.
(7) Explicitcapture: Ignores unnamed matching groups so that only explicit capturing groups are returned in the results list. Valid only when used with Regexmatch.
<ScriptBlock>
An expression that specifies the rule to which the delimiter applies. The expression must evaluate to a $true or $false. The script block needs to be enclosed in curly braces. For example:
PS d:\> $a = "Monday,tuesday,wesday,thurday,friday,saturday,sunday" PS d:\> $a-split {$_-eq "n"-or $_-eq "R"}Moda Y,tuesday,wesday,thuday,fiday,satuday,suday
as you can see from the above statement, look for the specified delimiter in the specified string (in this case, "n" or "R"), and perform a delimited operation if found.
2. Unary and two split operators
-split <string> This format is called a unary splitter operator, and it has precedence over commas. that is, if you submit a comma-delimited list of strings to a unary splitter operator, only the first string (the part that precedes the first comma) is split. For example:
PS d:\>-split "A B", "C D" ABC D
<string>-split <delimiter> This format is called the two-dollar split operator. You can enclose all the strings in parentheses, or store the strings in a variable, and then submit the variable to the split operator. For example:
PS d:\> "a B", "C D"-split "" Abcdps d:\>-split ("A B", "C D") Abcdps d:\> $a = "A B", "C D" PS d:\>-split $aabc D
Summarize
The split operator in PowerShell can delimit the specified string, meaning that the specified part of the string can be intercepted, which is often used to intercept the time format, or to intercept the file name and to make a fuzzy lookup.
This article from "Flower Blossom Fall" blog, declined reprint!
String split operator in
PowerShell