Powershell – Introducing ITPros to Developer Terms – Part 4 – Methods – padleft() / padright() / trim()

Powershell 

*** WARNING! ***
*** This Blog Post will talk about Developer Terms ***
*** ItPros are cautioned to break out their (Patent Pending) ***
*** “Peril Sensitive Sunglasses” (Ala the HitchHikers’ Guide to the Galaxy) ***
*** Before Reading further. If you are amongst the Bold and the DARING and the ***
*** Truly Devil May Care of ITPros, Please feel free to Read on and live Daring! ***

But don’t say I didn’t warn you 🙂
Sean – The Energized Tech

 

In Honor of St. Patrick’s Day, I am going to introduce you to two more methods, easy ones to use and learn.  And they’re cousins.

St. PaddyLeft() and St. PaddyRight() and they a…..

“HAWWWA HAWWWAA HWAWAWAHHAWA HWHAWHAHW HAWHAAWHAHHHAWAHAWWAWA”

Ok if the back section would clam up, it wasn’t THAT funny.  And honestly, who put Guiness on the cart today?

They are actually padleft() and padright().  Their tasks are simple and obvious sounding.  to “Pad” data, whether blanks or other data.

We won’t go into heavy detail on each one as they Do function identically, the only difference is whether data gets put to the left or the right.

So we have a sample Object.

$BEER=’Guiness’

Again, Piping $BEER through GET-MEMBER will show us a list of Properties and Methods, here are the two we are concerned with.

image

And again rerunning that same set of Methods with a Format-List which show us their definitions more clearly

image

Now these ones don’t look so bad.  Even the bunch at the back seem relaxed.  And that’s because they have two Definitions to work with, much like the “substring()” Method.

In both cases they are saying “Give me a Number to describe the Width I am padding” and the second Definition simply says “Yeah same as the first guy but tell me what CHARacter to be PADDING it with”

So $BEER.padleft(20)

Produces

             Guiness

Because what it is doing is Padding the Character string out so it is at least 20 spaces, and the “Padding” goes to the Left.

And doing a

$BEER.padright(12,”!”)

Gives us a

Guiness!!!!!

Since we are “Padding” it up to be at LEAST 12 characters. And we are padding it was an “!” for the filler.

Now this is all fine and dandy.  All this Padding on St. Paddy’s day.   But these so serve an important function that ALSO get’s introduced today.

Much like getting a nice haircut before heading out, You usually combine Padleft() and Padright() with Trim().

Trim has a definition so simple it’s not worth mentioning.  But because I like putting pretty pictures on the screen I’ll post it anyhow.

image

See two simple definitions, because it IS a simple method.   It “TRIMS” off the excess.

So with our previous example of $BEER.padright(12,”!”)

Guiness!!!!!

Running a trim() on top of that does two things.  If there are any blank spaces it will “Trim” them off (In Either Direction I might add). The other option in Trim is to specify what characters to Trim out of the left or the right of the object.

So if you had

!!!!!!!!Guiness!!!!!!!!!

in $BEER the result from running a

$BEER.Trim(“!”) would be

Guiness

And similiarly  if you had Spaces before or after like this in $BEER

         Guiness

Running a $BEER.trim() would strip off all spaces at the beginning and end to give you

Guiness

And you can even attach Methods together like this

$BEER.padright(12,”!”).Trim(“!”)

Would also give us

Guiness

Because it would execute the Padright(12,”!”) and then IMMEDIATELY after run a Trim(“!”) on the results.

A little confusing but not so much 🙂

That’s it folks.  There’s nothing more to it.   See?  Even the guys in the back didn’t fall asleep this time!  Methods aren’t so Nasty once you get used to them!

 

..and With all this talk of Guiness, I think I’ll have me one.  Happy St. Patrick’s Day

Sean
The Energized Tech

Leave a Reply