关于联接About join

简短说明SHORT DESCRIPTION

描述联接运算符 ( 联接) 如何将多个字符串合并为一个字符串。Describes how the join operator (-join) combines multiple strings into a single string.

详细说明LONG DESCRIPTION

联接运算符将一组字符串连接成一个字符串。The join operator concatenates a set of strings into a single string. 字符串将按照它们在命令中出现的顺序追加到结果字符串。The strings are appended to the resulting string in the order that they appear in the command.

语法Syntax

下图显示了联接运算符的语法。The following diagram shows the syntax for the join operator.

-Join <String[]>
<String[]> -Join <Delimiter>

parametersParameters

String []-指定要联接的一个或多个字符串。String[] - Specifies one or more strings to be joined.

分隔符-指定在串联的字符串之间放置的一个或多个字符。Delimiter - Specifies one or more characters placed between the concatenated strings. 默认值为 "" ) ( 分隔符。The default is no delimiter ("").

备注Remarks

一元联接运算符 ( 联接 <string [] >) 的优先级高于逗号。The unary join operator (-join <string[]>) has higher precedence than a comma. 因此,如果您将逗号分隔的字符串列表提交给一元联接运算符,则在第一个逗号) 之前仅 (第一个字符串提交到联接运算符。As a result, if you submit a comma-separated list of strings to the unary join operator, only the first string (before the first comma) is submitted to the join operator.

若要使用一元联接运算符,请将字符串括在括号中,或将字符串存储在变量中,然后提交要联接的变量。To use the unary join operator, enclose the strings in parentheses, or store the strings in a variable, and then submit the variable to join.

例如:For example:

-join "a", "b", "c"
a
b
c

-join ("a", "b", "c")
abc

$z = "a", "b", "c"
-join $z
abc

示例Examples

下面的语句联接了三个字符串:The following statement joins three strings:

-join ("Windows", "PowerShell", "2.0")
WindowsPowerShell2.0

下面的语句联接由空格分隔的三个字符串:The following statement joins three strings delimited by a space:

"Windows", "PowerShell", "2.0" -join " "
Windows PowerShell 2.0

以下语句使用多字符分隔符来联接三个字符串:The following statements use a multiple-character delimiter to join three strings:

$a = "WIND", "S P", "ERSHELL"
$a -join "OW"
WINDOWS POWERSHELL

下面的语句将下一个字符串中的行联接到一个字符串中。The following statement joins the lines in a here-string into a single string. 由于此字符串是一个字符串,因此必须先拆分此字符串中的行,然后才能联接它们。Because a here-string is one string, the lines in the here-string must be split before they can be joined. 您可以使用此方法重新加入已保存在以下字符串中的 XML 文件中的字符串:You can use this method to rejoin the strings in an XML file that has been saved in a here-string:

$a = @'
a
b
c
'@

(-split $a) -join " "
a b c

另请参阅SEE ALSO

about_Operatorsabout_Operators

about_Comparison_Operatorsabout_Comparison_Operators

about_Splitabout_Split