我想在二維數組中的數組的所有值中刪除前導0。修剪裏面的數值(一個數組(內部的(twoDimentionalArray))
有沒有圖書館可以爲我做這個?我試着用foreach循環,但它只修剪當前的臨時變量。
下面是代碼:
[String[]]$arr1 = @('str1','str2','str3')
[String[]]$arr2 = @('str4','str5','str6')
[String[]]$arr3 = @('0str7')
[System.Object[]]$twoDimensionalArray = @()
[System.Object[]]$twoDimensionalArray += ,($arr1)
[System.Object[]]$twoDimensionalArray += ,($arr2)
[System.Object[]]$twoDimensionalArray += ,($arr3)
Write-Host "Removing leading zeros from array in twoDimentionalArray..."
ForEach ($strValue in $twoDimensionalArray[2])
{
$strValue = $strValue.TrimStart('0')
}
Write-Host "Leading zero's are removed."
Write-Host ""
Write-Host '$strValue =' $strValue
Write-Host '$twoDimensionalArray[2] =' $twoDimensionalArray[2]
Write-Host ""
輸出(0str7)
Removing leading zeros from array in twoDimentionalArray...
Leading zero's are removed.
$strValue = str7
$twoDimensionalArray[2] = 0str7
所需的輸出(STR7)
Removing leading zeros from array in twoDimentionalArray...
Leading zero's are removed.
$strValue = str7
$twoDimensionalArray[2] = str7
任何想法?
看到這個答案,爲什麼你在做什麼不是一個二維數組http://stackoverflow.com/a/9397385/3829407 – Matt