如何在一个数组之间没有空格的数组?

时间:2017-05-25 19:58:31

标签: arrays powershell string-conversion

$readFile = get-content $readInput

#create an empty array to be filled with bank account numbers
$fNameArray = @()

for($i = 0; $i -lt $readFile.length; $i++){

    #assigns a random letter from the list to $letter.
    #$letter = get-random -inputobject ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z") -count $readFile.length 
    $letter = $readFile[$i] | foreach-object{get-random -inputobject ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z") -count $readFile[$i].length}

    $fnameArray += "$letter"
}
$fnameArray

代码正在读取一个包含名称列表并随机化数据屏蔽字母的文件。我遇到的唯一问题是输出是这样的:

L R Y E B
R O M I
U Q N G R
H K Y
M G A W Q
J G W Y D K T
X E Q
J Y P I G

它看起来像是在字母之间输出空格。我该如何消除它们?

1 个答案:

答案 0 :(得分:1)

-join运算符的一元形式连接(连接)所有数组元素而没有分隔符。

> -join ('a', 'b', 'c')
abc

因此,只需使用:

$fnameArray += -join $letter

相比之下,"$letter"使用$OFS(输出字段分隔符)作为分隔符对数组进行字符串化,默认为 space ,它解释了您的输出。

因此,您也可以将$OFS设置为''(空字符串)并使用"$letter"。 但是,-join方法更简单,并且不需要您为以前的$OFS值创建/恢复本地范围。