Reputation: 3534
this is my array :
eth1
eth12
eth5
eth11
When my string contains a number between 1 and 9 i want to add a 0 before, to obtain an array like this :
eth01
eth12
eth05
eth11
How can i achieve this? i don't know how to modify a string like this :/
Thanks
Upvotes: 0
Views: 129
Reputation: 26150
a more tortuous way than Ansgar's :)
$a=@("eth1","eth12","eth5")
$a|%{
$slice=$_.split("h");
if([int]$slice[1] -le 9){
$slice[1]="0"+$slice[1]
}
join-string -strings $slice -separator("h")
}
Upvotes: 1
Reputation: 200293
Like this:
$if = 'eth1', 'eth12', 'eth5', 'eth11'
$if -replace '(\D+)(\d)$', '${1}0$2'
Upvotes: 9