# arrays - variable - 將數組相對於兩端切割的慣用方法是什麼?

## powershell variable arrays (4)

Powershell的數組符號在切片數組末尾時有相當奇怪的行為，儘管有記錄。 官方文檔中的這一部分很好地總結了奇異性：

``````\$a[-3..-1]
``````

``````\$a[0..-2]
``````

``````\$a = 0,1,2,3
\$a[1..-1]
``````

``````1
0
3
``````

``````\$a[1..(\$a.Count-1)]
``````

``````a=1,2,3,4
a[1:-1]
``````

``````[array]\$A = (([int][char]'A')..([int][char]'Z')) | ForEach-Object {[char]\$_}
\$B = \$A[0..2]+\$A[-3..-1]
Clear-Host
Write-Host "Original List"
Write-Host \$A -NoNewline -Separator ', '
Write-Host
Write-Host "First three and last three"
Write-Host \$B -NoNewline -Separator ', '
``````

``````Original List
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
First three and last three
A, B, C, X, Y, Z
``````

``````\$a[1..(\$a.Count-1)]
``````

``````\$a = 0,1,2,3
[string]::Concat(\$a).Substring(1)
``````

\$ array [start..stop]其中stop是通過將數組的長度減去一個從數組末尾偏移的值來定義的：

``````\$a = 1,2,3,4,5,6,7,8,9
\$start = 2
\$stop = \$a.Length-3
\$a[\$start..\$stop]
``````

``````\$a = 1,2,3,4,5,6,7,8,9
\$a[2..(\$a.Length-3)]
``````