Reputation: 177
i'm trying to remove the last character of an array.
I have an array with:
name1;
name2;
Now i want to remove the last ";".
I'v though on something like:
$array = $array | Select -last 1 | foreach{$_.replace(";", "")}
But thats just an idea. Also not working, because it will leave only the last entry.
Upvotes: 2
Views: 3847
Reputation: 1884
To change just the last object, process every object of the array and check if it's the last. replace
only on the last one:
$array = $array | ForEach-Object {
if( $array.IndexOf($_) -eq ($array.count -1) ){
$_.replace(";","")
}else{$_}
}
Upvotes: 3
Reputation: 989
Try this to remove/replace last semi colon ';' from each element.
$array | foreach{ $_ -replace ';$', '' }
and if you want to remove the same from any where in the string
, just remove the $
Upvotes: 0