Powershell provides a merge operator for strings, the join operator-join a set of strings into a single string, and substrings are added to the resulting string in the order in which they appear in the command.
Join operators
The syntax for connection operators for strings in PowerShell is as follows:
-join <String[]><String[]>-join <Delimiter>
Parameter interpretation.
<String[]>
An array of substrings to concatenate, that is, which substrings are to be concatenated.
<Delimiter>
Specifies one or more characters to be placed between the concatenated strings. The default is an empty string (""). It is important to note that the split operator removes the delimiter in the split string, and the join operator inserts the specified delimiter into the concatenated string, and the split and merge are two relative operations.
Note that the -join <String[]> format is called a unary join operator, and it takes precedence over commas. Therefore, if you submit a comma-delimited list of strings to a unary join operator, only the first string (the part that precedes the first comma) is submitted to the join operator. For example:
PS c:\>-join "A", "C", "E" Ace
But if you want to use a unary join operator, you need to enclose the concatenated string in parentheses, or store the concatenated string in a variable, and then commit the variable to connect. For example:
PS c:\>-join ("A", "B", "C") Abcps c:\> $x = "A", "B", "C" PS c:\>-join $xabc
In addition to the join operator,-join, in the PowerShell arithmetic operator, the arithmetic operator "+" also has the connection function of the string, but the requirement is that the first operand must be a string, and no attempt is made to add the arithmetic operation. For example:
PS c:\> "A" + "B" + "C" ABC
In practice, the use of "+" for string connection is very convenient, but also very intuitive, but also the use of high-frequency string connection operation.
Here are three examples of string joins using-join:
PS c:\>-join ("Windows", "PowerShell", "5.0") windowspowershell5.0ps c:\> "Windows", "PowerShell", "5.0"-join "" Windows Powershell 5.0PS c:\> $x = "Wind", "SP", "Ershell" PS c:\> $x-join "ow" Windowspowershell
Finally, the use of the delimited operator-split and the join operator-join to convert multiple lines of content to a single line, for example:
PS c:\> $a = @ ' >> a>> b>> c>> ' @>>ps c:\> (-split $a)-join "" A B C
A $ A = @ ' @ ' @ is called here-string, which treats the content between two @ as a string, which is a special operator in PowerShell. In actual use, the XML file content is more than one line, This method can be used to re-join the XML file in the here-string saved in the string .
Summarize
The-join keyword is used to concatenate strings in Powershell, and you can use the arithmetic operator "+" to concatenate strings, but only if the first operand must be a string. Joins and splits are opposing operations, such as merging can use to create a file name that specifies the name of the time "+", while the time and the specified name may come from different places.
This article from "Flower Blossom Fall" blog, declined reprint!
(19) PowerShell string Merge operator