about_Join
主題
about_join
簡短描述
說明聯結運算子 (-join) 如何將數個字串結合成單一字串。
完整描述
聯結運算子會將一組字串串連成單一字串。字串會依照其呈現在命令中的順序,附加至產
生的字串。
語法
下表顯示聯結運算子的語法。
-Join <String[]>
<String[]> -Join <Delimiter>
參數
String[]
指定一個或多個要聯結的字串。
分隔字元
指定一個或多個要放在串連字串之間的字元。
預設不使用分隔符號 ("")。
註解
一元聯結運算子 (-join <string[]>) 的優先順序高於逗號。因此,
如果您提交逗號分隔的字串清單給一元聯結運算子,只有第一個字串 (位
於第一個逗號前) 會提交給聯結運算子。
若要使用一元聯結運算子,請以括號括住字串,或將字串儲存在變數中,然後提交變數給
join。
例如:
-join "a", "b", "c"
a
b
c
-join ("a", "b", "c")
abc
$z = "a", "b", "c"
-join $z
abc
範例
下列陳述式會聯結三個字串:
-join ("Windows", "PowerShell", "2.0") WindowsPowerShell2.0
下列陳述式會聯結三個字串,並以空格分隔:
"Windows", "PowerShell", "2.0" -join " " Windows PowerShell 2.0
下列陳述式使用多個字元的分隔符號來聯結三個字串:
$a = "WIND", "SP", "ERSHELL"
$a -join "OW"
WINDOWSPOWERSHELL
下列陳述式會將 here-string 的各行聯結成單一字串。因為 here-string 是一個
字串,所以必須先分割 here-string 中的各行,然後才能進行聯結。您可以使用這
個方法,針對已儲存為 here-string 的 XML 檔案,重新聯結檔案中的字串:
$a = @'
a
b
c
'@
(-split $a) -join " "
a b c
請參閱
about_Operators
about_Comparison_Operators
about_Split