简体   繁体   English

Powershell - 删除两个变量之间的空格

[英]Powershell - Remove space between two variables

I have two array's which contain a selection of strings with information taken from a text file. 我有两个数组,其中包含一系列字符串,其中包含从文本文件中获取的信息。 I then use a For Loop to loop through both arrays and print out the strings together, which happen to create a folder destination and file name. 然后,我使用For循环遍历两个数组并将字符串打印在一起,这恰好创建了一个文件夹目标和文件名。

Get-Content .\PostBackupCheck-TextFile.txt | ForEach-Object { $a = $_ -split ' ' ; $locationArray += "$($a[0]):\$($a[1])\" ; $imageArray += "$($a[2])_$($a[3])_VOL_b00$($a[4])_i000.spi" }

The above takes a text file, splits it into separate parts, stores some into the locationArray and other information in the imageArray , like this: 上面是一个文本文件,将它分成不同的部分,将一些存储到locationArray并将其存储在imageArray ,如下所示:

locationArray[0] would be L:\\Place\\ locationArray[0]将是L:\\Place\\

imageArray[0] would be SERVERNAME_C_VOL_b001_i005.spi imageArray[0]将是SERVERNAME_C_VOL_b001_i005.spi

Then I run a For Loop: 然后我运行一个For循环:

for ($i=0; $i -le $imageArray.Length - 1; $i++) 
    {Write-Host $locationArray[$i]$imageArray[$i]}

But it places a space between the L:\\Place\\ and the SERVERNAME_C_VOL_b001_i005.spi 但是它在L:\\Place\\SERVERNAME_C_VOL_b001_i005.spi之间放置了一个空格

So it becomes: L:\\Place\\ SERVERNAME_C_VOL_b001_i005.spi 所以它变成: L:\\Place\\ SERVERNAME_C_VOL_b001_i005.spi

Instead, it should be: L:\\Place\\SERVERNAME_C_VOL_b001_i005.spi 相反,它应该是: L:\\Place\\SERVERNAME_C_VOL_b001_i005.spi

How can I fix it? 我该如何解决?

Option #1 - for best readability: 选项#1 - 为了最好的可读性:

{Write-Host ("{0}{1}" -f $locationArray[$i], $imageArray[$i]) }

Option #2 - slightly confusing, less readable: 选项#2 - 略显混乱,不太可读:

{Write-Host "$($locationArray[$i])$($imageArray[$i])" }

Option #3 - more readable than #2, but more lines: 选项#3 - 比#2更易读,但更多行:

{
  $location = $locationArray[$i];
  $image = $imageArray[$i];
  Write-Host "$location$image";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM