如何将元素之间没有空格的数组字符串化?
How dow I stringify an array without spaces between its elements?
$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
看起来是字母之间有空格输出的。我该如何消除它们?
-join
运算符的一元形式连接(串联)所有数组元素,没有分隔符。
> -join ('a', 'b', 'c')
abc
因此,只需使用:
$fnameArray += -join $letter
相比之下,"$letter"
使用 $OFS
(输出字段分隔符)作为分隔符对数组进行字符串化,默认为 space,它解释了你的输出。
因此,您也可以将 $OFS
设置为 ''
(空字符串)并使用 "$letter"
。
但是,-join
方法更简单,不需要您为/恢复以前的 $OFS
值创建本地作用域。
$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
看起来是字母之间有空格输出的。我该如何消除它们?
-join
运算符的一元形式连接(串联)所有数组元素,没有分隔符。
> -join ('a', 'b', 'c')
abc
因此,只需使用:
$fnameArray += -join $letter
相比之下,"$letter"
使用 $OFS
(输出字段分隔符)作为分隔符对数组进行字符串化,默认为 space,它解释了你的输出。
因此,您也可以将 $OFS
设置为 ''
(空字符串)并使用 "$letter"
。
但是,-join
方法更简单,不需要您为/恢复以前的 $OFS
值创建本地作用域。