PowerShell Snippet: Trimming The String!


When building up urls from different parameters in something like TeamCity, or Octopus, it’s simple enough to get double “//” in urls if the parameters are not consistent. So little helper functions are always useful to have imported to manage such things. Below is an example of such a thing!

function Update-Parameter
[bool] $trimstart = $false,
[bool] $trimend = $false

if ($trimstart)
$StringChecker = $param.StartsWith($character)
if ($StringChecker -eq "True")
$param = $param.TrimStart($character)
if ($trimend)
$StringChecker = $param.EndsWith($character)
if ($StringChecker -eq "True")
$param = $param.TrimEnd($character)
return $param

$url = "https://academy.microsoft.com/"
$lan = "/en-us/"
$ext = "/professional-program/data-science/"
$trim = "/"

$output = "Before Trim {0}/{1}/{2}" -f $url, $lan, $ext

$url = Update-parameter $url $trim $false $true
$lan = Update-parameter $lan $trim $true $true
$ext = Update-Parameter $ext $trim $false $true

$output = "After Trim {0}/{1}/{2}" -f $url, $lan, $ext

Author: Richie Lee

Full time computer guy, part time runner. Full time Dad, part time blogger. Pokémon Nut. Writer of fractured sentences. Maker of the best damn macaroni cheese you've ever tasted.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s