Pad IP addresses with leading 0's - powershell
问题 I'm looking to pad IP addresses with 0's example 1.2.3.4 -> 001.002.003.004 50.51.52.53 -> 050.051.052.053 Tried this: [string]$paddedIP = $IPvariable [string]$paddedIP.PadLeft(3, '0') Also tried split as well, but I'm new to powershell... 回答1: You can use a combination of .Split() and -join . ('1.2.3.4'.Split('.') | ForEach-Object {$_.PadLeft(3,'0')}) -join '.' With this approach, you are working with strings the entire time. Split('.') creates an array element at every . character. .PadLeft